top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How do I modify the commit message (not the topmost one) of a git commit?

+1 vote
864 views

I did a series of commits and now I find one of my commit (not the topmost one) has an incorrect commit message. How can I change that specific one? I believe "git commit --amend" works only for the last commit.

posted May 1, 2013 by Natarajan Venkatraman

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
"git rebase -i"

4 Answers

0 votes

You can use git rebase, for example, if you want to modify back to commit bbc643cd, run

$ git rebase --interactive bbc643cd^

In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify. Make your changes and then stage them with

$ git add <filepattern>

Now you can use

$ git commit --amend

to modify the commit, and after that

$ git rebase --continue

to return back to the previous head commit.

answer May 1, 2013 by Salil Agrawal
0 votes

you can use the interactive rebase to change a commit message. For this, get the commits hash of the commit before the one you want to change. For example, if you have the following commits:
A -> B -> C -> D -> E -> F
where F is the newest commit and you want to change the commit message from commit D, then you will need to get the hash from commit C. With this, use the command git rebase -i hash-C. It will open an editor
with commits D, E and F. To change D commits message, change the word that appears in front of D from pick to reword.

answer May 4, 2013 by anonymous
0 votes
Git rebase -i [commit id] 

Can be good to take a backup and read the manual before doing this the first time...

answer May 4, 2013 by anonymous
0 votes

True. Be aware that "git commit --amend" and the rebasing shown below
amounts to rewriting history, which is fine when it's your own local
history, but if you've already shared the commits you are changing,
then you're forcing your collaborators to rebase their own work on top
of your rewritten history.

That said, here's how to do a rebase to change a specific commit
message:

  1. Make sure you have a clean worktree. "git stash" any uncommitted
    changes.

  2. Identify the commit you want to change, e.g. by using something
    like HEAD~5 (for the 5th last commit on your current branch), or by
    the 40-character commit id.

  3. "git rebase -i $commit^", where $commit is the commit you
    identified above, and the trailing caret "^" is important. (rebase
    works on a range of commits, and you want to start the rebase based on
    the parent of the commit you wish to change).

  4. The rebase command will open your text editor with a list of your
    commits, from the one you'd like to change, to your most recent
    commit. Each commit is prefixed with the word "pick", which indicates
    that rebase will replay that commit without any changes. You want to
    change the commit message for the first commit in this list, so on the
    first line, change the "pick" into "reword". Save and exit the editor.

  5. Rebase will now start replaying the commits from your list. The
    first thing that will happen, is that it will reopen your text editor
    with the commit message for the commit you want to change. Edit the
    commit message to your liking, and save and exit the editor.

  6. Rebase will then replay all the following commits until the last
    commit is done, and you're "back" where you were when you started the
    rebase. Since you did nothing more than change a commit message, you
    will not get any conflicts during the rebase.

  7. If you stashed some uncommitted changes in step #0, you might want
    to un-stash them now: "git stash pop"

Hope this help

answer May 4, 2013 by anonymous
Similar Questions
0 votes

After I resolved conflict, how can I do commit by using ".git/MERGE_MSG" without editor ?

I did like below but It didn't work.

$ cat .git/MERGE_MSG | git commit -m 

Is there any way?

+2 votes

Could somebody tell me how to make git rebase -i show diff of squashed commits (for example), like git commit -v does it?

+1 vote

When I run git clean -xfd, git deletes my tags file. I have the tag file listed in gitignore, but how do I tell git not to remove the tags file.

+1 vote

I have added some changes on commit message in prepare-commit-msg file and then I exec this command

git config --global commit.template .git/hooks/prepare-commit-msg  

After that when I do git commit I receive something like this

40 lines of my changes and then  

# Please enter the commit message for your changes. Lines starting # with # will be ignored, 
  and an empty message aborts the commit. # Explicit paths specified without -i nor -o; 
  assuming --only paths... # On branch master # Changes to be committed: # # modified: test #  

Question is there any chance to show this default message on the top? Or better permanently remove this message?

...