Mercurial undo last commit

139,886

Solution 1

One way would be hg rollback (deprecated as of Hg2.7, August 2013)

Please use hg commit --amend instead of rollback to correct mistakes in the last commit.

Roll back the last transaction in a repository.

When committing or merging, Mercurial adds the changeset entry last.
Mercurial keeps a transaction log of the name of each file touched and its length prior to the transaction. On abort, it truncates each file to its prior length. This simplicity is one benefit of making revlogs append-only. The transaction journal also allows an undo operation.

See TortoiseHg Recovery section:

alt text

This thread also details the difference between hg rollback and hg strip:
(written by Martin Geisler who also contributes on SO)

  • 'hg rollback' will remove the last transaction. Transactions are a concept often found in databases. In Mercurial we start a transaction when certain operations are run, such as commit, push, pull...
    When the operation finishes succesfully, the transaction is marked as complete. If an error occurs, the transaction is "rolled back" and the repository is left in the same state as before.
    You can manually trigger a rollback with 'hg rollback'. This will undo the last transactional command. If a pull command brought 10 new changesets into the repository on different branches, then 'hg rollback' will remove them all. Please note: there is no backup when you rollback a transaction!

  • 'hg strip' will remove a changeset and all its descendants. The changesets are saved as a bundle, which you can apply again if you need them back.

ForeverWintr suggests in the comments (in 2016, 5 years later)

You can 'un-commit' files by first hg forgetting them, e.g.: hg forget filea; hg commit --amend, but that seems unintuitive.
hg strip --keep is probably a better solution for modern hg.

Solution 2

hg strip will completely remove a revision (and any descendants) from the repository.

To use strip you'll need to install MqExtension by adding the following lines to your .hgrc (or mercurial.ini):

[extensions]
mq =

In TortoiseHg the strip command is available in the workbench. Right click on a revision and choose 'Modify history' -> 'Strip'.

Since strip changes the the repository's history you should only use it on revisions which haven't been shared with anyone yet. If you are using mercurial 2.1+ you can uses phases to track this information. If a commit is still in the draft phase it hasn't been shared with other repositories so you can safely strip it. (Thanks to Zasurus for pointing this out).

Solution 3

Since you can't rollback you should merge that commit into the new head you got when you pulled. If you don't want any of the work you did in it you can easily do that using this tip.

So if you've pulled and updated to their head you can do this:

hg --config ui.merge=internal:local merge

keeps all the changes in the currently checked out revision, and none of the changes in the not-checked-out revision (the one you wrote that you no longer want).

This is a great way to do it because it keeps your history accurate and complete. If 2 years from now someone finds a bug in what you pulled down you can look in your (unused but saved) implementation of the same thing and go, "oh, I did it right". :)

Solution 4

hg rollback is what you want.

In TortoiseHg, the hg rollback is accomplished in the commit dialog. Open the commit dialog and select "Undo".

alt text

Solution 5

In the current version of TortoiseHg Workbench 4.4.1 (07.2018) you can use Repository - Rollback/undo...:
enter image description here

Share:
139,886
Martin Buberl
Author by

Martin Buberl

Updated on April 13, 2021

Comments

  • Martin Buberl
    Martin Buberl about 3 years

    How can I undo my last accidentally commited (not pushed) change in Mercurial?

    If possible, a way to do so with TortoiseHg would be prefered.

    Update

    In my concrete case I commited a changeset (not pushed). Then I pulled and updated from the server. With these new updates I decided, that my last commit is obsolete and I don't want to sync it. So it seems, that hg rollback is not exactly what I'm searching for, because it would rollback the pull instead of my commit.

  • Martin Buberl
    Martin Buberl over 13 years
    When I understand that correct the rollback removes the last transaction. In my case I did a commit, then I pulled from the server. So, does that mean I would rollback the pull instead of commit?
  • Tim Henigan
    Tim Henigan over 13 years
    @Martin Buberl: According to selenic.com/mercurial/hg.1.html#rollback, pull is considered a transaction. So if you pulled, executing hg rollback will undo the pull instead of the commit.
  • Martin Buberl
    Martin Buberl over 13 years
    I made a commit, then a pull and update/sync. With these new changes I've got I decided my last commit is obsolet and want to undo it. The Undo button in TortoiseHg is disabled in my case.
  • Martin Buberl
    Martin Buberl over 13 years
    Thanks, I'm going to edit my question a little bit to point that better ou.
  • GazB
    GazB almost 12 years
    IF the revision in question is still in the draft phrase (or you have access to ALL of the repo's that it has been push/pulled to and apply the strip to all of them) (which is the case in this description) then this is sooo the best and cleanest option.
  • David
    David about 11 years
    If you have TortoiseHg installed, there's a graphical interface for globally activating mq. In the current version: enable "file->settings->global settings tab->extensions->mq" or access the settings file through the button "Edit file".
  • UpTheCreek
    UpTheCreek almost 10 years
    Rollback is now deprecated.
  • rbrtl
    rbrtl over 8 years
    In the current version of Tortoise HG you can get to the Rollback/Undo option from the Repository menu item.
  • Tim Tisdall
    Tim Tisdall over 8 years
    I'm not sure how hg commit --amend is supposed to work, but for me it gave me an editor to modify the commit message and no method to change which files were included. I exited the editor without saving and found that my mistaken commit was changed to include ALL changed files!
  • DanMan
    DanMan almost 8 years
    @UpTheCreek citation needed.
  • UpTheCreek
    UpTheCreek almost 8 years
    @DanMan - check the link he provided.
  • ForeverWintr
    ForeverWintr over 7 years
    It's not clear to me how to accomplish an answer to the question using hg commit --amend. You can 'un-commit' files by first hg forgetting them, e.g.: hg forget filea; hg commit --amend, but that seems unintuitive. hg strip --keep is probably a better solution for modern hg.
  • VonC
    VonC over 7 years
    @ForeverWintr Thank you. I have included your comment in the answer for more visibility.
  • Neptilo
    Neptilo over 7 years
    As pointed out by Tim Tisdall and ForeverWintr, using hg commit --amend to solve the problem is confusing and just doing that won't work in the general case. The hg strip answer should be put forward and highlighted more. I prefer upvoting Peter Graham's answer for that reason.
  • Jason S
    Jason S over 6 years
    This doesn't tell you how to use hg commit --amend to undo a commit. I want to bring my repo to the state before the last commit (don't care if the "bad" commit is in the history). In SVN I would just do a reverse-merge. How do I do this in Hg?
  • VonC
    VonC over 6 years
    @JasonS book.mercurial-scm.org/read/… is a good read, although in your case, you could also consider hg reset: stackoverflow.com/a/31302998/6309