DEV Community

Cover image for Changing a Git commit Message
letsbuild
letsbuild

Posted on

Changing a Git commit Message

If a commit message contains unclear, incorrect, or sensitive information, you can amend it locally and push a new commit with a new message to GitHub. You can also change a commit message to add the missing information.

Rewriting the most recent commit message

You can change the most recent commit message using the git commit --amend command.

In Git, the text of the commit message is part of the commit. Changing the commit message will change the commit ID--i.e., the SHA1 checksum that names the commit. Effectively, you are creating a new commit that replaces the old one.

The commit has not been pushed online

If the commit only exists in your local repository and has not been pushed to GitHub, you can amend the commit message with the git commit --amend command.

  1. On the command line, navigate to the repository that contains the commit you want to amend.
  2. Type git commit --amend and press Enter.
  3. In your text editor, edit the commit message, and save the commit.

The new commit and message will appear on GitHub the next time you push.

You can change the default text editor for Git by changing the core.editor setting.

The bellow command will change the default git editor to vs code.

git config --global core.editor "code --wait"

Amending older or multiple commit messages

If you have already pushed the commit to GitHub, you will have to force push a commit with an amended message. [* this is not recommended as people who have already cloned your repository will have to manually fix their local history]

Changing the message of the most recently pushed commit

  1. Follow the steps above to amend the commit message.
  2. Use the push --force-with-lease command to force push over the old commit.

git push --force-with-lease example-branch

Changing the message of older or multiple commit messages

If you need to amend the message for multiple commits or an older commit, you can use interactive rebase, then force push to change the commit history.

  1. On the command line, navigate to the repository that contains the commit you want to amend.
  2. Use the git rebase -i HEAD~n command to display a list of the last n commits in your default text editor.

# Displays a list of the last 3 commits on the current branc
$ git rebase -i HEAD~3h

The list will look similar to the following:

pick e499d89 Delete CNAM
pick 0c39034 Better README
pick f7fde4a Change the commit message but push the same commit.

# Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# 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 
Enter fullscreen mode Exit fullscreen mode

3.Replace pick with reword before each commit message you want to change.

pick e499d89 Delete CNAM
reword 0c39034 Better README
reword f7fde4a Change the commit message but push the same commit.
Enter fullscreen mode Exit fullscreen mode

4.Save and close the commit list file.

5.In each resulting commit file, type the new commit message, save the file, and close it.

6.When you're ready to push your changes to GitHub, use the push --force command to force push over the old commit.

git push --force example-branch
Enter fullscreen mode Exit fullscreen mode

Here is a YT video on git: Git Crash Course 2021 with GitHub

Discussion (0)