Git How to Change the Commit Message (Step-by-Step Guide)

To change the commit message of the most recent commit, you can use the git commit –amend -m command:

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

For new Git users: Changing the commit message means rewriting history. If you haven’t pushed the commit yet, renaming won’t cause problems. But to rename a pushed commit, you need to be careful. If your teammate has pulled your repo, they won’t be able to pull it after you change the message!

This is a comprehensive guide to changing commit messages in Git. You will learn how to change the commit message of:

  • The most recent commit.
  • An arbitrary commit.
  • A pushed commit.

And more.

Unpushed Commits

Let’s take a look at changing the commit messages of unpushed commits first.

Because the commits are local, changing the name or names is straightforward and easy. All you need to do is run a command that changes a commit message or messages and continue with the project.

Later on, you will see how changing a pushed commit is a bit trickier.

1. Change the Commit Message of the Most Recent Commit

Renaming the most recent local commit might be the most common use case for renaming commits in Git. This is because you might typo the commit message or simply reword it before actually pushing it.

Luckily, changing the commit message of an unpushed local commit is also the most straightforward task to do.

To change the commit message of the most recent local commit, use git commit –amend -m:

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

The –amend flag makes it possible to change the most recent commit. The –amend flag combines the staged changes with the previous commit instead of creating a new commit.

If you have changes you want to add to the previous commit, you can use git commit –amend. Changing the commit message is one of the most popular use cases for the –amend option.

Example

Let’s see a concrete example of changing the name of a git commit.

Here’s a demo project where the two last commits add files file2 and file3 to the project.

Let’s say you want to change the message of the last commit to something a bit more informative.

To do this, run the git commit –amend command as follows:

git commit --amend -m "Add a text file called file3.txt"

After running this command, the git log commit history looks like this:

As you can see, the last commit message is now different!

Because you’re changing a local commit, there’s nothing else you need to do after amending the new name. However, if this commit was already pushed to the remote origin, this wouldn’t be the case. You will learn about changing pushed commit messages in just a bit.

2. Change the Commit Message of Multiple Commits

To change the commit message of commits other than the most recent one, you cannot use the –amend option. Instead, you need to use the git rebase -i HEAD~n command to show and change the n recent commits.

Example

Let’s see a concrete example by continuing with the demo project you already saw in the previous example.

In the first example, you removed the most recent commit. In this example, you’ll rename the second commit “Add file 2” to match the style of the first commit.

To rename the second commit, you need to use the interactive rebasing, git rebase -i. Because you want to access the 2nd most recent commit, you need to call the interactive rebase to HEAD~2, which will show the 1st and the 2nd commits in an interactive view:

git rebase -i HEAD~2

This opens up an interactive view which looks a bit verbose it’s the first time you see it:

Notice how I changed “pick” to “reword” in the top-left corner on the line of the commit message you want to change. This tells the interactive shell that you want to change the name of that particular commit.

When you save this change, a new interactive window opens up. In this view, you give a new commit message to the commit you want to change:

After saving, you can check what the git log looks like. In this case, both the second recent commit message was changed:

Now, let’s move on to pushed commits.

Pushed Commits

Changing the commit message of a pushed commit is similar to changing a local commit. But the process is a bit different especially if you’re working in a team. This is because changing the commit message of an earlier commit means changing the history of the project. If your team members have pulled your branch before you change a commit message, they’ll face issues when pulling the next time.

With proper commands and team communication, you can change the name of a commit or commits with relative ease. This section shows you how to change the most recent pushed commit as well as how to change multiple pushed commits.

3. Change the Commit Message of a Pushed Commit

Changing the most recent pushed commit is straightforward, but it requires a forced push and will cause issues if someone tries to pull the changes.

First, you need to use the git commit –amend -m command to locally change the commit message:

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

To push the changed commit message, you need to do a force-push by:

git push --force <remoteName> <branchName>

So for example, you might do:

git push --force origin main

Now you’ve done your part.

But someone that now tries to pull the changes will face issues. This is because you just changed the commit history of the project by changing the contents of an existing commit.

Typically, a developer who wants to pull these changes will need to do a fetch + rebase by running:

git pull --rebase

4. Change the Commit Message of Multiple Pushed Commits

To change names of multiple pushed commits, you need to do a git rebase -i HEAD~n for n most recent commits. This is nothing new to you as you already saw an example of this earlier in this guide.

git rebase -i HEAD~n

But because the commits already exist in the remote origin, you cannot just push the commits with new messages. Instead, you need to do a force push. (From this point on, the idea is exactly the same as in the previous section):

git push --force <remoteName> <branchName>

Besides, you need to communicate with your team that there was a forced push. This is because, after a force push, your teammates cannot run git pull to get your changes. Instead, they’ll usually have to do a pull with rebase.

git pull --rebase

Summary

Today you learned how to change the name of commits in Git.

To take home, changing the most recent local commit is the easiest. All you need to do is run git commit –amend -m “New message”.

To change many commits’ messages, you can use git rebase -i HEAD~n to modify n most recent commits.

But if you’re touching a pushed commit (or commits), you’re changing the project history in Git. This means you need to do a force push. Besides, your teammates cannot pull the code with git pull. So you need to carefully communicate the changes among your team so that they can pull the forced updates without losing work.

Thanks for reading. Happy coding!

Read Also

How to Undo Git Commit