How do you "rollback" last commit on Mercurial?

MercurialCommitRollback

Mercurial Problem Overview


I have a Mercurial repository that I use in local only... It's for my personal usage (so I don't "push" anywhere).

I made a commit with 3 files, but after that I understood that I should do commit 4 files...

Is there a way to "rollback" my last (latest, only one) commit, and "recommit" it with the correct files?

(I don't know why, but my "Amend current revision" option is not active, so I can't use it...)

Mercurial Solutions


Solution 1 - Mercurial

You just need this command:

hg rollback

See: http://hgbook.red-bean.com/read/finding-and-fixing-mistakes.html.

(Technically, this is deprecated as of version 2.7, August 2013, but I've yet to see an alternative that does exactly the same thing.)

Solution 2 - Mercurial

The answer is strip (if you don't have it enabled you can check how to enable it here: https://stackoverflow.com/a/18832892/179581).

If you want to revert just the latest commit use:

hg strip --keep -r .

If you want to revert to a specific commit:

hg strip --keep -r 1234

Using strip will revert the state of your files to the specified commit but you will have them as pending changes, so you can apply them together with your file to a new commit.

Recover your stripped data:

If you miss-used the command or you want to recover your changes you can find your stripped files in the .hg/strip-backup folder.

Tutorial on how to restore your files, or just google for it (works the same on all OS).

Credit to ForeverWintr

Solution 3 - Mercurial

In modern hg:

hg uncommit

or, for your exact problem:

hg add file4
hg amend

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionserhioView Question on Stackoverflow
Solution 1 - MercurialMartin EdenView Answer on Stackoverflow
Solution 2 - MercurialMugur 'Bud' ChiricaView Answer on Stackoverflow
Solution 3 - MercurialMartin C. MartinView Answer on Stackoverflow