Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recover from "hg update" with uncommitted changes

I run into the following issue all the time with Mercurial, and it's very annoying:

  • I'm at some revision A.
  • I have local changes, which I meant to commit or amend on top of A, but haven't yet.
  • I want to go to some revision B, but I forgot that I had local changes!
  • I do hg update B. Mercurial "helpfully" tries to rebase my local changes to apply on top of B. This typically results in conflicts, and it now asks me to fix the conflicts.

However, I don't want to fix the conflicts! I don't want my local changes to apply on top of B at all. I want them to stay at A, either as a new commit just after A, or amended into A, as the case may be.

Is there a way I can recover from this state? The only way I know how is to

  1. fix the merge conflicts at B
  2. go back to A, getting merge conflicts again
  3. fix the merge conflicts again at A
  4. commit my changes at A and go back to B

This is a lot of work, and it's pointless. I shouldn't have to rebase my local changes to apply on top of B, only to rebase them again to apply on top of A.

If there's no better way to recover from this mistake, is there a way to get hg to refuse to do an update when you have local changes? I never want to do that - if I wanted that I'd just commit the local changes and rebase them on top of B.

like image 618
HighCommander4 Avatar asked Mar 13 '14 22:03

HighCommander4


People also ask

How do you remove 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.

What is hg revert?

hg revert changes the file content only and leaves the working copy parent revision alone. You typically use hg revert when you decide that you don't want to keep the uncommited changes you've made to a file in your working copy.

How do I revert to a previous commit in Heartgold?

If you want to revert changes already committed: To backout a specific changeset use hg backout -r CHANGESET . This will prompt you directly with a request for the commit message to use in the backout. To revert a file to a specific changeset, use hg revert -r CHANGESET FILENAME .

What is hg command?

Description. The hg command provides a command line interface to the Mercurial system.


2 Answers

Getting the dirty files back after updating somewhere and back is a bit tricky. The "trick" is to make sure that your working copy has the dirty files after the first update.

So after you do

hg update $SOMEWHERE

and discover the mess because Mercurial begins opening merge tools, calmly close the merge tools and then run

hg resolve --unmark --all
hg resolve --all --tool internal:local

All files that were merged because you had changes in them will now look like they did in your dirty working copy. This includes files that were merged cleanly and files you were prompted to merge. Updating back is now possible:

hg update $BACK
hg resolve --unmark --all
hg resolve --all --tool internal:local

You should now be back to where you started. If you modified the files after the first update, then it is the modified version you see after the second resolve. This is why you will want to resolve the files after the first update.

like image 168
Martin Geisler Avatar answered Sep 24 '22 02:09

Martin Geisler


hg update -c will abort the update if you have any uncommitted changes.

like image 29
Jon Avatar answered Sep 24 '22 02:09

Jon