Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mercurial: undo automatic merge with uncommited changes

I did hg pull and hg update on a folder where I had uncommited changes. It opened the three-way compare/merge tool for a couple of files, but I closed them all since I did not remember whether the changes were older or newer than what was in the repo - I thought that this will leave the files as conflicted, like it would do in SVN .. but no, all the files got merged:

updating to branch default
25 files updated, 0 files merged, 0 files removed, 0 files unresolved

Unfortunately, the merge mixed the two versions in a way that does not work.

My question is therefore if there is a way to undo the merge and get back the local files (with the uncommited changes) as they were before I called hg update?

(I found this post, where one commenter suggests hg resolve --tool internal:local, but it does not seem to do anything in my case.)

Thanks.

like image 238
Michal Kaut Avatar asked Nov 13 '13 19:11

Michal Kaut


People also ask

How do you revert uncommitted changes in mercurial?

You can undo all uncommitted changes by doing: $ hg revert somefile # undo uncommitted changes to this file $ hg revert -a # undo all uncommitted changes to all files.


1 Answers

As you've found out the hard way, Mercurial will merge the changes in the working copy into the target revision when you update.

If the merge was clean (from the point of view of Mercurial), the affected files are marked as "resolved". You can check this with

$ hg resolve --list

It will show a bunch of files with a R for resolved. Unresolved files are listed with U. Before hg resolve will work on a file, you have to mark it as unresolved first:

$ hg resolve --unmark your-file

Here is a full session where I create a commit with two commits:

$ hg init
$ echo x > x
$ hg commit -A -m x
adding x
$ echo xx >> x
$ hg commit -m xx

I then update to the first commit (where the file contains x) and change it to y:

$ hg up 0
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ echo y > x

Updating back to the second commit triggers the merge, which succeeds:

$ hg up 
merging x
0 files updated, 1 files merged, 0 files removed, 0 files unresolved
$ hg resolve --list
R x
$ cat x
y
xx

Before I can get my file back, I need to mark it as unresolved. I can then resolve it using the internal:local tool:

$ hg resolve --unmark x
$ hg resolve --tool internal:local x
$ cat x
y

The merge state is stored in .hg/merge. There you'll find a bunch of files names by SHA-1 hashes. I suggest making a copy of this directory before you begin playing with the above commands since Mercurial deletes the directory when it thinks it is no longer needed.

like image 182
Martin Geisler Avatar answered Oct 27 '22 00:10

Martin Geisler