74

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

Neeme Praks
  • 8,956
  • 5
  • 47
  • 47
serhio
  • 28,010
  • 62
  • 221
  • 374
  • 1
    possible duplicate of [Mercurial undo last commit](http://stackoverflow.com/questions/4760684/mercurial-undo-last-commit) – ᄂ ᄀ Dec 23 '14 at 16:28
  • use `hg strip --keep -r .` http://stackoverflow.com/a/19064016/1286571 – ForeverWintr Dec 01 '16 at 02:27
  • Possible duplicate of [Mercurial (hg) equivalent of git reset (--mixed or --soft)](http://stackoverflow.com/questions/13112280/mercurial-hg-equivalent-of-git-reset-mixed-or-soft) – ForeverWintr Dec 01 '16 at 02:37
  • For those who come here later, it's important the note about "amend current revision option is not active". **Amend current revision is the best option** if you can do it. – PhoneixS Mar 08 '18 at 11:29

3 Answers3

98

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

Martin Eden
  • 6,143
  • 3
  • 30
  • 33
  • 3
    I find how to do this by interface: Repository=>Rollback\Undo (Ctrl+U). Thank you! – serhio Dec 09 '13 at 12:53
  • 9
    hg rollback is deprecated, hg commit --amend should be used. Please see http://stackoverflow.com/questions/4760684/mercurial-undo-last-commit – ᄂ ᄀ Dec 23 '14 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. – user2864740 Dec 07 '15 at 03:49
  • (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.) – user2864740 Dec 07 '15 at 03:56
  • @user2864740 I don't know where you found the statement about replacement. For the task in question `hg commit --amend` works just fine. – ᄂ ᄀ Dec 07 '15 at 07:50
  • 1
    You can use `hg commit --amend -i` to only selectively add changes. – Reimer Behrends Dec 07 '15 at 08:11
  • 2
    $ hg commit --amend -i results with "hg commit: option -i not recognized". – kguest Jan 06 '16 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. – ForeverWintr Oct 21 '16 at 21:46
39

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

Mugur 'Bud' Chirica
  • 4,246
  • 1
  • 31
  • 34
  • 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 Oct 18 '17 at 14:24
  • 3
    Strip is an extension which is not enabled by default. See https://stackoverflow.com/a/18832892/179581 how to enable it – Andrei May 06 '18 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. – Assad Ebrahim Oct 15 '20 at 03: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? – Violet Giraffe Jul 26 '22 at 19:51
5

In modern hg:

hg uncommit

or, for your exact problem:

hg add file4
hg amend
Martin C. Martin
  • 3,565
  • 3
  • 29
  • 36