mercurialcommitmercurial-commit

Mercurial: how to amend the last commit?


I'm looking for a counter-part of git commit --amend in Mercurial, i.e. a way to modify the commit which my working copy is linked to. I'm only interested in the last commit, not an arbitrary earlier commit.

The requirements for this amend-procedure are:

Update (1):

Update (2):


Solution

  • You can now use the --amend option with hg commit to update the last commit with the current working directory, since the release of Mercurial 2.2.

    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 "Phases" feature to prevent updates that would change history that's already been made available outside of the local repository.