Saturday, December 25, 2010

Changing commit message of a revision in Mercurial

About 2-3 months back I started using Mercurial for version control. If you have not heard of Mercurial before, this post will not be of any interest to you. Just know that Mercurial is great and I strongly recommend it if in your work you feel the need to keep different versions of a file at different points of time, and the way you do this is by saving the file under different names. See here for more information on Mercurial. For the rest, read below.

As I started with Mercurial, from the very beginning I had a feeling that one day I would make a mistake in recording the message that accompanies a commit. It seemed very likely that I would forget to make note of some important change I had made to a file. Of course, there was also the possibility of typos. This is what happened a couple of days back; I committed a revision with an incomplete and partly incorrect message. I had intended to do something, changed my mind while I was modifying the file, but wrote a message which reflected my original intentions and hit enter. What made the problem worse is that I did not realize this until I had made a couple of subsequent commits. I searched on Google to find how I could fix my mistake. I found a couple of ways, but they seemed a little confusing, especially given my lack of experience with version control systems. Here I explain the method that I felt was the easiest and most straightforward to use. This involves using the histedit extension for Mercurial. The extension can do more than just change a commit message, but I will focus only on this functionality, hoping that this will make the process clearer to follow.
  1. Install histedit.
    If you have already installed histedit, you can skip this step.
    1. Download histedit.
      • Open the terminal and navigate to the directory where you want to save histedit. For
        example, I have saved it in ~/Dropbox/Mercurial_Extensions.
      • At the terminal prompt type:
        hg clone 
        This will download the histedit extension in your present working directory.
    2. Tell your Mercurial installation about the location of this extension.
      • Open .hgrc file.
        This file is usually located in your HOME directory. If so, navigate to your home folder by typing cd ~ at the terminal prompt and pressing enter.
      • Then type open .hgrc and press enter.
    3. Add the following lines in the .hgrc file.
      histedit = ~/Dropbox/Mercurial_add_ons/histedit/
      Save the file.
      Note that you may have to modify ~/Dropbox/Mercurial_add_ons/
      part of the path to point to the directory where you downloaded
      and saved the histedit extension.
  2. Navigate to the directory where the hg repository whose history you want to change is located.
  3. Ensure that none of the files that Mercurial is tracking have been modified since the last commit. If there are modified files you have to commit the changes.
  4. At the terminal prompt type hg log and press enter. Note the changeset information of the revision whose commit message you want to modify. You will need this information in the next few steps.
  5. Suppose you want to change the commit message of
    changeset:  5:48f58ff8250e
    At the terminal prompt, either type:
    hg histedit -r 5
    and press enter or, type
    hg histedit 48f58ff8250e
    and press enter.

    This will open a file with a list of changesets in a text editor. The first changeset is the one that you specified. The list will look something like the one shown in the following image.

  6. In the first column of this file, replace the word pick with the word edit in the row corresponding to the changeset whose commit message you want to edit. Do not touch the other rows. Save the file at the default location and under the default filename suggested by your editor.
  7. At the terminal prompt type hg histedit --continue and press enter. A file will open in a text editor with your old commit message. Make any changes to this message and save the file (again, use the suggested file name and location). The old message in the revision history will be replaced by your corrected message.

Go here for more information on histedit.


  1. Good, thanks!
    But I guess this only change the history on the local repository, right? Have an idea about how to extend it to the remote one?

  2. @Diego you can't change the history of a remote repository, for the same reason you can't commit to a remote repository : you have to push and then go to the remote repository and merge (and update).

    If you want to edit the history of a remote repository, you have to call histedit directly on it.

  3. Very nice post as it

  4. You can use MQ extension, instead, which already comes with Mercurial:

    Imagine that you have 500 commits, and your erroneous commit message is in r.498.

    hg qimport -r 498:tip
    hg qgoto 498.diff
    hg qrefresh -e
    (change the comment in the editor, right after the mercurial header)
    hg qgoto 500.diff
    hg qfinish 498.diff:500.diff

  5. Thank you very much.

    Worked very fine for me!

  6. Thanks works great.

  7. Thanks for posting this, worked perfectly.