Mercurial: how to amend the last commit?

I'm looking for a counter-part of git commit --amend in Mercurial, ie a way to modify the commit which my working copy is linked to. The requirements for this amend-procedure are:

  • if possible, it should not require any extensions. It must not require non-default extensions , ie extensions which do not come with an official Mercurial installation.

  • if the commit to amend is one head of my current branch, no new head should be created. If the commit is not head, a new head may be created.

  • the procedure should be safe in a way that if for whatever reasons the amending fails, I want to have the same working copy and repository state restored as before the amending. With other words, if the amending itself can fail, there should be a fail-safe procedure to restore the working copy and repository state. I'm referring to "failures" which lie in the nature of the amend-procedure (like eg conflicts), not to file-system-related problems (like access restrictions, not being able to lock a file for writing, ...)

  • Update (1):

  • the procedure must be automatable , so it can be performed by a GUI client without any user interaction required.
  • Update (2):

  • files in the working directory must not be touched (there may be file system locks on certain modified files). This especially means, that a possible approach may at no point require a clean working directory.

  • With the release of Mercurial 2.2, you can use the --amend option with hg commit to update the last commit with the current working directory

    From the command line reference:

    The --amend flag 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 old commit is stored in a backup bundle in .hg/strip-backup (see hg help bundle and hg help unbundle on how to restore it).

    Message, user and date are taken from the amended commit unless specified. When a message isn't specified on the command line, the editor will open with the message of the amended commit.

    The great thing is that this mechanism is "safe", because it relies on the relatively new "Phases" feature to prevent updates that would change history that's already been made available outside of the local repository.


    You have 3 options to edit commits in Mercurial:

  • hg strip --keep --rev -1 undo the last (1) commit(s), so you can do it again (see this answer for more information).

  • Using the MQ extension, which is shipped with Mercurial

  • Even if it isn't shipped with Mercurial, the Histedit extension is worth mentioning

  • You can also have a look on the Editing History page of the Mercurial wiki.

    In short, editing history is really hard and discouraged . And if you've already pushed your changes, there's barely nothing you can do, except if you have total control of all the other clones.

    I'm not really familiar with the git commit --amend command, but AFAIK, Histedit is what seems to be the closest approach, but sadly it isn't shipped with Mercurial. MQ is really complicated to use, but you can do nearly anything with it.


    GUI equivalent for hg commit --amend :

    This also works from TortoiseHG's GUI (I'm using v2.5):

    Swich to the 'Commit' view or, in the workbench view, select the 'working directory' entry. The 'Commit' button has an option named 'Amend current revision' (click the button's drop-down arrow to find it).

    在这里输入图像描述

              ||
              ||
              /
    

    在这里输入图像描述

    Caveat emptor :

    This extra option will only be enabled if the mercurial version is at least 2.2.0, and if the current revision is not public, is not a patch and has no children. [...]

    Clicking the button will call 'commit --amend' to 'amend' the revision.

    More info about this on the THG dev channel

    链接地址: http://www.djcxy.com/p/6410.html

    上一篇: 模块maven项目与嵌套模块

    下一篇: Mercurial:如何修改最后一次提交?