Thursday, April 29, 2010

Changing erroneous git commit message...

Just the other day, I was making a few small changes to my new site and noticed -- just before I pushed them to github -- that I had created a bad commit message: somehow I had copied the text from the previous commit. Not Good. I figured that would be easy to change and -- while it turned out to be quite easy -- it wasn't easy to find how to do it. Googling under various phrases kept getting me things like:

"...just use git rebase -i and 'edit' the commit." While that's quite true, unless you really get git, it's like the old Microsoft helicopter joke. (I suppose if I really read through the excellent documentation provided for git, I would have been able to figure it out, but that's a lot of reading for a quick fix.)

I finally had some help from my friend Wolf Arnold and with a little bit of trial and error, I was able to modify my commit message and then push my 3 commits to github. Here's how to do it. NOTE: This procedure only works if you have not pushed your commits. Once pushed, the commit message is inviolable - by design - as far as I can tell.
  1. First, make sure you know which commit you want to change. I used gitk (for Linux; gitx on the Mac), which gives you a visual display of your commits and the status of the remotes you are pushing to. Find the relative position of the commit you want to change. In my case, it was the 2nd on the list -- that is, one down from the top.
  2. In your root project directory (where you normally type your "git status" and other commands), type the following:
    git rebase -i HEAD~n
    where 'n' is equal to your commit's position on the list, counting from 1 at the top of the list. Notice that the character between HEAD and n is the "twiddle" character or "tilde".
  3. Once you enter that command, you are presented with a screen (in your favorite editor) that looks something like this (the numbers following 'pick' are the internal ids for your commits):
    pick <lots of numbers>
    pick <lots of different numbers>
    # Rebase 9a122e0..9a122e0 onto 9a122e0
    # Commands:
    #  p, pick = use commit
    #  e, edit = use commit, but stop for amending 
    #  s, squash = use commit, but meld into previous commit 
    # # If you remove a line here THAT COMMIT WILL BE LOST.
    # However, if you remove everything, the rebase will be aborted.
  4. At this point, you can do one of several things:
    1. Edit the file and change the word "pick" to "edit" on the top line of the file. That's the commit that you want to change. Save the file and exit the editor. That will set you up to make the changes in your commit.
    2. Delete every line in the file and save/close it. That's equivalent to a No-Op" and will terminate the rebase.
    3. There are more options, as you can see from the instructions in the file itself, but we'll leave that for another day.
  5. When the edit finishes, you will receive instructions at the command line.  The next step in the process is to run the command:
    git commit --amend
    and you will again be presented with an edit screen where you can change the commit message (or other things, but I didn't get into that either). Just change the line with the commit message to what you want it to be.
  6. Once that's done and you've saved/closed the file, run the command:
    git rebase --continue
    and it'll be done. You can use gitk to check things out and then push to whatever remote you're using.


No comments:

Post a Comment