75

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...)

4

3 Answers 3

100

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.)

8
  • 3
    I find how to do this by interface: Repository=>Rollback\Undo (Ctrl+U). Thank you!
    – serhio
    Commented Dec 9, 2013 at 12:53
  • 9
    hg rollback is deprecated, hg commit --amend should be used. Please see stackoverflow.com/questions/4760684/mercurial-undo-last-commit
    – ᄂ ᄀ
    Commented Dec 23, 2014 at 16:27
  • 8
    @fnt It's a shame that hg commit --amend simply isn't a replacement for hg 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 using hg commit --amend as a general hg revert replacement. Commented Dec 7, 2015 at 3:49
  • 2
    $ hg commit --amend -i results with "hg commit: option -i not recognized".
    – kguest
    Commented Jan 6, 2016 at 14:21
  • 13
    @fnt: Your comments would be more useful if you showed how hg commit --amend can be used to replace hg rollback in this case. Commented Oct 21, 2016 at 21:46
41

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

4
  • this 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)
    – Molik Miah
    Commented Oct 18, 2017 at 14:24
  • 3
    Strip is an extension which is not enabled by default. See stackoverflow.com/a/18832892/179581 how to enable it
    – Andy
    Commented May 6, 2018 at 20:18
  • To 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. Commented Oct 15, 2020 at 3:16
  • What happens if I pull changes from remote, strip the last changeset and then push back (or make a new commit and push)? Does that remove the commit from the remote history? Does that break the repo? Commented Jul 26, 2022 at 19:51
6

In modern hg:

hg uncommit

or, for your exact problem:

hg add file4
hg amend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.