91

I goofed up a merge. I'd like to revert then try again.
Is there a way to revert a merge before it is committed?

hg revert doesn't do what I'd like, it only reverts the text of the files. Mercurial aborts my second attempt at merging and complains original merge is still uncommitted.

Is there a way to undo a merge after an hg merge command but before it's committed?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
deft_code
  • 57,255
  • 29
  • 141
  • 224
  • Possible duplicate of [How to abandon a hg merge?](http://stackoverflow.com/questions/2570087/how-to-abandon-a-hg-merge) – Rodrigue Feb 03 '16 at 10:59

3 Answers3

113

hg update -C <one of the two merge changesets>

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • Simple, efficient and, I think, the best way to do it! :D – Eric-Karl Oct 21 '10 at 23:09
  • @JonL. - Yes, `--clean` is a synonym for `-C`. I'm not sure which changeset `.` will refer to, but it will always be one of the two merged changesets. – Omnifarious Jan 30 '12 at 17:37
  • 4
    @Omnifarious, cleaning `.` should clean to whatever revision you were on prior to initiating the merge. So if you were at r42, attempted to merge with r43, failed, cleaned, you'd be back on a pristine r42. – Jon L. Feb 03 '12 at 15:49
  • 3
    `hg update --clean` works, as `.` seems to be the the implicit default - it's [not explicitly documented](http://www.selenic.com/mercurial/hg.1.html#update) though. – Joel Purra Sep 27 '12 at 14:48
  • @JoelPurra: The meaning of `.` in the context of a merge is rather ambiguous. – Omnifarious Sep 27 '12 at 15:25
  • @Omnifarious: seemed to default to the working copy changeset that was there before any other changeset was `merge`'d *in* as the second parent. Good enough for me =) – Joel Purra Sep 27 '12 at 15:53
  • @JoelPurra: I should look in the code to see if that's the explicit intention. The other possibility I could see is the default being the changeset with the lowest or highest valued hash (which is essentially picking one randomly). – Omnifarious Sep 27 '12 at 18:39
43

After you do hg merge, but before hg commit, your working copy has two parents: the first parent is the changeset you had updated to before the merge and the second parent is the changeset you are merging with. Mercurial will not let you do hg merge again as long as your working copy has two parents.

You have two options on how to proceed:

  1. If you want to abort the merge and get back to where you started, then do

    hg update -C .
    

    This will update the working copy to match the first parent: the . always denotes the first parent of the working copy.

  2. If you want to re-merge some files then do

    hg resolve fileA fileB
    

    This will re-launch the merge tools just as when you did hg merge. The resolve command is good if you find out at hg merge-time that your merge tools are configured badly: fix the configuration and run hg resolve --all. You can run hg resolve as many times as you want until you are satisfied with the merge.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
2

Today there is hg merge --abort. See hg help merge.

mljrg
  • 4,430
  • 2
  • 36
  • 49