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.
- Install histedit.
If you have already installed histedit, you can skip this step. - 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 https://bitbucket.org/durin42/histedit
This will download the histedit extension in your present working directory. - 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.
- Open .hgrc file.
- Add the following lines in the .hgrc file.
[extensions] histedit = ~/Dropbox/Mercurial_add_ons/histedit/hg_histedit.py
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.
- Navigate to the directory where the hg repository whose history you want to change is located.
- 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.
- 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.
- 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.
- 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.
- 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.
Good, thanks!
ReplyDeleteBut I guess this only change the history on the local repository, right? Have an idea about how to extend it to the remote one?
No. Sorry, I do not.
ReplyDelete@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).
ReplyDeleteIf you want to edit the history of a remote repository, you have to call histedit directly on it.
Very nice post ..works as it
ReplyDeleteAwesome. Saved my skin!
ReplyDeleteYou can use MQ extension, instead, which already comes with Mercurial:
ReplyDeleteImagine 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
Thank you very much.
ReplyDeleteWorked very fine for me!
Thanks works great.
ReplyDeleteThanks for posting this, worked perfectly.
ReplyDelete