Senthess
Senthess

Reputation: 17540

Is there another way of removing multiple heads?

Let's say I have this:

hg init

touch a
hg add a
hg commit -m a

touch b
hg add b
hg commit -m b

hg up -r 0

touch c
hg add c
hg commit -m c

Now I will have multiple heads, because of the last commit. If, for example I want to keep the last head, the one created by commit c ( effectively discarding b, and all other commits made after the first ) , how could I do it? I played a little with mq's strip command, and I'm able to achieve what I want, but I'd like to know if there's another way.

Upvotes: 3

Views: 822

Answers (2)

David Joyner
David Joyner

Reputation: 23157

You can also perform a dummy merge of the changeset containing b (changeset 1 in this case):

$ hg --config ui.merge=internal:fail merge 1
resolving manifests
getting b
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)

$ hg revert --all --no-backup --rev .
removing b

$ hg status
R b

$ hg commit -m "Dummy merge -- eliminate b"
committed changeset 3:c163151f19df

$ ls
a  c

Note that in this case you haven't rewritten any history or created any new repos -- particularly important if b had already been pushed to a centralized repo and pulled by others.

Upvotes: 2

Joel B Fant
Joel B Fant

Reputation: 24746

Yes, there is another way. From your example above, the output of hg glog looks a bit like this:

@  changeset:   2:925573c7103c
|  tag:         tip
|  parent:      0:4fe26dfe856d
|  user:        Joel B Fant
|  date:        Thu Jul 28 23:20:45 2011 -0400
|  summary:     c
|
| o  changeset:   1:9dc928176506
|/   user:        Joel B Fant
|    date:        Thu Jul 28 23:20:24 2011 -0400
|    summary:     b
|
o  changeset:   0:4fe26dfe856d
   user:        Joel B Fant
   date:        Thu Jul 28 23:20:12 2011 -0400
   summary:     a

If you clone test but specify a revision, it will only clone that revision and its ancestors. So if your repo's directory is TwoHeadsMightNotBeBetter, you can go to its parent directory and issue:

hg clone TwoHeadsMightNotBeBetter OneHeadIsFine -r 925573c7103c

I specified changeset id in that, but you could also use -r 2 instead, or even -r tip since it is currently the tip of that repo. Now when you go into the new clone and do hg glog, you have only one head:

@  changeset:   1:925573c7103c
|  tag:         tip
|  user:        Joel B Fant
|  date:        Thu Jul 28 23:20:45 2011 -0400
|  summary:     c
|
o  changeset:   0:4fe26dfe856d
   user:        Joel B Fant
   date:        Thu Jul 28 23:20:12 2011 -0400
   summary:     a

If you had 3 heads and wanted to keep 2, it would be a little different. You'd have to clone one of them with -r and the also specify -r on hg pull to pull specific branches.

Upvotes: 2

Related Questions