Right ways to change a GIT commit message

Posted by Gulab Bisht

Every developer uses git these days and sometimes make the mistake of putting wrong commit message. And the next step is always to find the right way to edit the git commit message.

So here is the small reference guide to modifying GIT commit messages based on different scenarios.

Change last git commit message

The most common situation is when you put a wrong commit message and want to change the last git commit message. Here are two ways to do it.

Option 1:

The first way is to use amend. This will open the editor where you can simply change your last commit message.

Step 1:

git commit --amend

Using the above command you can also set the commit message from the command line itself:

git commit --amend -m "Updated message."

Step 2:

This step is only required if you have already pushed the wrong commit message. You just need to push the changed commit message with force.

git push <remote> <branch> -f

Option 2:

Another way to update the git commit message is to use git rebase. This way enables you to update old commit messages as well.

Step 1:

Simply run the git rebase command with -i and HEAD~1 argument to update the last commit message.

git rebase -i HEAD~1

Step 2:

The above command will prompt an edit window where you can simply use "r" or "e" to reword or edit the commit message.

pick 85777d5 Wrong commit message.

# Rebase 767c24c..85777d5 onto 767c24c (1 command)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Edit multiple git commit messages

To update multiple commit message above command can be use with little modification. Just replace the n with the desired number of git commit messages you want to update.

git rebase -i HEAD~n

This will give you an edit window to update multiple messages at the same time.

pick ded8f04 Commit message 1.
pick ba30cf0 Commit message 2.
pick 767c24c Commit message 3.
pick 85777d5 Commit message 4.

# Rebase 6c8eb83..85777d5 onto 6c8eb83 (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

 

Here are some more reference guides to help you understand the concepts better.

https://www.atlassian.com/git/tutorials/rewriting-history

https://www.atlassian.com/git/tutorials/rewriting-history#git-rebase-i

Tags