version control - revise - How to edit incorrect commit message in Mercurial?

mercurial revise commit (8)

Last operation was the commit in question

To change the commit message of the last commit when the last mercurial operation was a commit you can use

$ hg rollback

to roll back the last commit and re-commit it with the new message:

$ hg ci -m 'new message'

But be careful because the rollback command also rolls back following operations:

  • import
    • pull
    • push (with this repository as the destination)
    • unbundle

(see hg help rollback)

Thus, if you are not sure if the last mercurial command was a hg ci, don't use hg rollback.

Change any other commit message

You can use the mq extension, which is distributed with Mercurial, to change the commit message of any commit.

This approach is only useful when there aren't already cloned repositories in the public that contain the changeset you want to rename because doing so alters the changeset hash of it and all following changesets.

That means that you have to be able to remove all existing clones that include the changeset you want to rename, or else pushing/pulling between them wouldn't work.

To use the mq extension you have to explicitly enable it, e.g. under UNIX check your ~/.hgrc, which should contain following lines:


Say that you want to change revision X - first qimport imports revisions X and following. Now they are registered as a stack of applied patches. Popping (qpop) the complete stack except X makes X available for changes via qrefresh. After the commit message is changed you have to push all patches again (qpop) to re-apply them, i.e. to recreate the following revisions. The stack of patches isn't needed any, thus it can be removed via qfinish.

Following demo script shows all operations in action. In the example the commit message of third changeset is renamed.

set -x -e -u
echo INFO: Delete old stuff
rm -rf .hg `seq 5`
echo INFO: Setup repository with 5 revisions
hg init
echo '[ui]' > .hg/hgrc
echo 'username=Joe User <[email protected]>' >> .hg/hgrc
echo 'style = compact' >> .hg/hgrc
for i in `seq 5`; do
  touch $i && hg add $i && hg ci -m "changeset message $i" $i
hg log 
echo INFO: Need to rename the commit message or the 3rd revision
echo INFO: Displays all patches
hg qseries
echo INFO: Import all revisions including the 3rd to the last one as patches
hg qimport -r 2:tip
hg qseries
echo INFO: Pop patches
hg qpop 2.diff
hg qseries
hg log 
hg parent
hg qrefresh -m 'CHANGED MESSAGE'
hg log 
echo INFO: Push all remaining patches
hg qpush -a
hg log 
hg qseries
echo INFO: Remove all patches
hg qfinish -a
hg qseries && hg log && hg parent

Copy it to an empty directory an execute it e.g. via:

$ bash 2>&1 | tee log

The output should include the original changeset message:

+ hg log
2   53bc13f21b04   2011-08-31 17:26 +0200   juser
  changeset message 3

And the rename operation the changed message:

+ hg log
2   3ff8a832d057   2011-08-31 17:26 +0200   juser

(Tested with Mercurial 1.7.5)

This question already has an answer here:

I am currently using TortoiseHg (Mercurial) and accidentally committed an incorrect commit message. How do I go about editing this commit message in the repository?

Update: Mercurial has added --amend which should be the preferred option now.

You can rollback the last commit (but only the last one) with hg rollback and then reapply it.

Important: this permanently removes the latest commit (or pull). So if you've done a hg update that commit is no longer in your working directory then it's gone forever. So make a copy first.

Other than that, you cannot change the repository's history (including commit messages), because everything in there is check-summed. The only thing you could do is prune the history after a given changeset, and then recreate it accordingly.

None of this will work if you have already published your changes (unless you can get hold of all copies), and you also cannot "rewrite history" that include GPG-signed commits (by other people).

As others have mentioned the MQ extension is much more suited for this task, and you don't run the risk of destroying your work. To do this:

  1. Enable the MQ extension, by adding something like this to your hgrc:
    mq =
  2. Update to the changeset you want to edit, typically tip:
    hg up <rev>
  3. Import the current changeset into the queue:
    hg qimport -r .
  4. Refresh the patch, and edit the commit message:
    hg qrefresh -e
  5. Finish all applied patches (one, in this case) and store them as regular changesets:
    hg qfinish -a

I'm not familiar with TortoiseHg, but the commands should be similar to those above. I also believe it's worth mentioning that editing history is risky; you should only do it if you're absolutely certain that the changeset hasn't been pushed to or pulled from anywhere else.

Good news: hg 2.2 just added git like --amend option.

and in tortoiseHg, you can use "Amend current revision" by select black arrow on the right of commit button

In TortoiseHg, right-click on the revision you want to modify. Choose Modify History->Import MQ. That will convert all the revisions up to and including the selected revision from Mercurial changesets into Mercurial Queue patches. Select the Patch you want to modify the message for, and it should automatically change the screen to the MQ editor. Edit the message which is in the middle of the screen, then click QRefresh. Finally, right click on the patch and choose Modify History->Finish Patch, which will convert it from a patch back into a change set.

Oh, this assumes that MQ is an active extension for TortoiseHG on this repository. If not, you should be able to click File->Settings, click Extensions, and click the mq checkbox. It should warn you that you have to close TortoiseHg before the extension is active, so close and reopen.

Rollback-and-reapply is realy simple solution, but it can help only with the last commit. Mercurial Queues is much more powerful thing (note that you need to enable Mercurial Queues Extension in order to use "hg q*" commands).

There is another approach with the MQ extension and the debug commands. This is a general way to modify history without losing data. Let me assume the same situation as Antonio.

// set current tip to rev 497
hg debugsetparents 497
hg debugrebuildstate
// hg add/remove if needed
hg commit
hg strip [-n] 498