How do you "rollback" last commit on Mercurial?
Solution 1
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
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
In modern hg:
hg uncommit
or, for your exact problem:
hg add file4
hg amend
serhio
I like .NET and started learning PHP. email: gserhio[at]gmail[.]com
Updated on May 09, 2020Comments
-
serhio about 4 years
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...)
-
serhio over 10 yearsI find how to do this by interface: Repository=>Rollback\Undo (Ctrl+U). Thank you!
-
ᄂ ᄀ over 9 yearshg rollback is deprecated, hg commit --amend should be used. Please see stackoverflow.com/questions/4760684/mercurial-undo-last-commit
-
user2864740 over 8 years@fnt It's a shame that
hg commit --amend
simply isn't a replacement forhg rollback
. While it can be used to add new changes (or change the message), good luck getting it to not add changes.. "[amend] can be used to amend the parent of the working directory with a new commit that contains the changes in the parent in addition to those currently reported by hg status, if there are any." The TortoiseHG Workbench does some magic to allow 'record like' hunk selection which can indeed to this: but that is not by usinghg commit --amend
as a generalhg revert
replacement. -
user2864740 over 8 years(The problem in the
hg commit --amend
case is that, without going the Mq route, Hg simply lacks a Git-like staging area as a concept.) -
ᄂ ᄀ over 8 years@user2864740 I don't know where you found the statement about replacement. For the task in question
hg commit --amend
works just fine. -
Reimer Behrends over 8 yearsYou can use
hg commit --amend -i
to only selectively add changes. -
u2107680007911543774e7r over 8 years$ hg commit --amend -i results with "hg commit: option -i not recognized".
-
ForeverWintr over 7 years@fnt: Your comments would be more useful if you showed how
hg commit --amend
can be used to replacehg rollback
in this case. -
Molik Miah over 6 yearsthis worked perfectly for me. including '--keep' brings those undone commit files back into the pending state so you can discard/shelve or commit again. thank you. (by the way, i used SourceTree)
-
Andrei about 6 yearsStrip is an extension which is not enabled by default. See stackoverflow.com/a/18832892/179581 how to enable it
-
Assad Ebrahim over 3 yearsTo enable the strip extension in TortoiseHg 4.7 or above, use File > Settings > Extensions then checkbox strip, and restart TortoiseHg. Use View > ShowConsole to show the Hg command line to type in e.g. hg strip --keep -r 50.