How to Change Commit Message in Git

Learn how you can modify Git commit messages

You may often be in a situation where you need to change a commit message in Git. You probably made a typo in your commit message or need to edit it to adequately capture the changes made in the commit. You may have also accidentally added sensitive data or just want to abide by your team’s policy by writing your Git commit message in a specific format. Whatever scenario you find yourself in, we will show you how you can modify Git commit messages.

Key Takeaways

  • You can change the latest commit message in Git by using the git commit command with the --amend option.
  • You can change several commit messages by using the git rebase command interactively.
  • It is not recommended to change the message of a commit that’s already in a remote repository.

Try the Git Log Lab for free

Git Log Lab
Git Log Lab

Why Is Changing A Git Commit Message To Be Clear So Important?

A commit message in Git explains the code changes made in a specific commit. Without it, you would find it quite difficult to track or understand the historical changes made to a Git repository.

Changing a Git commit message to make it clear and descriptive is important and has some of the following benefits:

  • Communication: It helps you to collaborate better with other team members during code review, for example. It helps other team members understand the rationale behind a code change when reviewing code.
  • Troubleshooting: Also, when you are debugging an issue, a clearly written commit message can help to identify which commit is the root cause of a bug.
  • Release notes: When commit messages are meaningful, it makes creating release notes much easier.
  • Clear Historical reference: A clear history shows how a project evolves over time. It provides clear contexts as to why certain changes were made.
  • Identifying features: A clear commit message in a repository’s history helps you easily identify what commits are associated with a particular feature.

Want a quick intro to Git and how to set it up in your system? Check out this video:

We have seen why having a clear commit message in Git is important. Let’s delve right in to see how we can modify Git commit messages.

How To Change The Last Commit Message

To change the most recent commit message that is still on your local Git repository, you can run the Git command below:

git commit --amend -m "Add commit message here"

This changes the latest commit message. The -m option lets you write a new commit message without Git opening your default text editor. To see the change, you can run the Git command below:

git log

You can type :q to exit.

Furthermore, if the commit you amended is already in a remote repository, then you will need to forcefully push it to the remote. Assuming you are currently on the develop branch, run the Git command below to do this:

git push origin develop --force

From above, the remote repository (i.e. origin) will be updated to reflect the updated commit message.

Git makes the operation of amending the latest commit that already exists in the Git repository possible, but it is not recommended. Please check the FAQ section to understand why amending an already pushed commit is not recommended.

Let’s now see how to change the messages for multiple commits in Git.

How To Change Multiple Commits Messages

You may also want to change multiple or previous Git commit messages. You can achieve this using git rebase interactively.

The git rebase command can be run in an interactive mode, which gives you the opportunity to change commits. It’s a powerful Git tool for rewriting history.

To change the commit messages of the last 3 commits, for example, run the following command:

git rebase -i HEAD~3

From above, the option -i means interactive. HEAD~3 is a relative commit reference, which means the last 3 commits from the current branch HEAD.

The git rebase command above will open your default text editor (Vim for example) in an interactive mode showing the last 3 commits like below:

From above, you can see the several commands you can use during a Git rebase session. However, our focus is on the reword command highlighted on line 9, which is used to edit commit messages during a rebase operation. Lines 1 to 3 show the last 3 commits, which have some typos in their commit messages. For the commit messages you want to edit, change the word pick to reword by pressing i to go into the insert mode as seen below:

Then, press esc to leave the insert mode, followed by :wq to save and quit.

Next, Git opens up your text editor again for each commit for you to update the commit message:

You can see the updated commit messages when you run the git log command.

As seen in the previous section, if you change the message of a commit that’s already in the remote repository, you will need to forcefully push it to the remote using git push origin <current_ branch> --force

FAQ

Below is a frequently asked question about editing commit messages in Git.

It is highly discouraged because it can cause code conflicts for other team members. Anytime you change a commit message, the commit hash is replaced, which makes these technically new commits. Hence, changing commits that are already in the remote repository is not recommended.

Conclusion

The ability to change commit messages in Git is very useful because it allows you to have a clear and descriptive Git history. You have learned how to achieve this by using the relevant Git commands presented in this article. However, it is advised to use these commands cautiously to avoid any confusion.

Are you interested in learning Git? Check out our Git for Beginners Course, which covers the fundamentals you need to get up and running with Git. It also includes simple visualizations, animations, and hands-on labs to help you internalize concepts, git commands, and work with Git as you would in a real-world environment.

Check our DevOps path to get started with your DevOps career today!!!