Mercurial undo last commit

后端 未结 8 665
既然无缘
既然无缘 2020-12-02 06:11

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<

相关标签:
8条回答
  • 2020-12-02 06:22

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

    0 讨论(0)
  • 2020-12-02 06:23

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

    0 讨论(0)
  • after you have pulled and updated your workspace do a thg and right click on the change set you want to get rid of and then click modify history -> strip, it will remove the change set and you will point to default tip.

    0 讨论(0)
  • 2020-12-02 06:32

    I believe the more modern and simpler way to do this now is hg uncommit. Note this leaves behind an empty commit which can be useful if you want to reuse the commit message later. If you don't, use hg uncommit --no-keep to not leave the empty commit.

    hg uncommit [OPTION]... [FILE]...

    uncommit part or all of a local changeset

    This command undoes the effect of a local commit, returning the affected
    files to their uncommitted state. This means that files modified or
    deleted in the changeset will be left unchanged, and so will remain
    modified in the working directory.
    
    If no files are specified, the commit will be left empty, unless --no-keep
    

    Sorry, I am not sure what the equivalent is TortoiseHg.

    0 讨论(0)
  • 2020-12-02 06:40

    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.

    0 讨论(0)
  • 2020-12-02 06:40

    Its workaround.

    If you not push to server, you will clone into new folder else washout(delete all files) from your repository folder and clone new.

    0 讨论(0)
提交回复
热议问题