Signup/Sign In

How to Amend Git Commit Message

A commit is a snapshot of our project at an instant in time. Commits form the core of version control as they allow us to save different versions of our project and go back to these versions whenever we want. Each commit is associated with a message and this message gives us information about the changes we have stored in that commit.

There may be some cases where we want to alter this message. Let's learn how to make changes to a commit message.

Changing commit messages

Amending(Update) Commit Messages

We can alter the commit messages by using the Git Commit command with the --amend option. However, this will only work for the most recent commit of our current branch. If we wish to change the message of some other previous commit we will have to use the Git Rebase command in the Interactive Mode.

Git commit --amend

The Git Commit command is used to add commits to our repository. By using the --amend option we can alter the last commit that we made. We can use it to change the commit message and even add or modify the files that were included in the last commit.

$ git commit --amend

The above command will open your configured text editor and you can change the commit message of the most recent commit. We can use the -m flag with the above command to enter the new message without opening the editor.

$ git commit --amend -m "New Commit Message"

A thing to note is that the above commands will not alter the previous commit. An entirely new commit is created with the changes that we have made and this new commit replaces the existing one. We can see this change in the following example.

After changing the commit message we can see that even the commit hash has been changed. This indicates that a new commit was created and the older one was discarded.

Checking the history of commits before changing the commit message

After altering the commit message an entirely new commit is created with a different commit hash.

Git Rebase --Interactive

The Git Commit command can only be used to alter the message of the last commit. We can use the Git Rebase command with the -i or the --interactive option to change the message of other commits. To use the Git Rebase command we will need a reference to the parent of the commit that we want to alter.

For example, if we have 3 commits on a branch(A --- B --- C) and want to alter the second last commit(B), we will need the hash of the commit A. We can also use the HEAD~n notation to accomplish this. For this example, we will be using HEAD~2 as commit A is the 2nd commit before our HEAD(commit C). Let's learn how to use the Git Rebase command.

  • First, run the command with the hash of the parent of the commit that you want to alter. We can also use the HEAD~n notation.
$ git rebase -i <commit-hash>
$ git rebase -i HEAD~n
  • After running the Git Rebase command, Git will open a text editor that will have all the commits made after the commit that we mentioned in the command.

  • Choose the commit whose message you want to alter and replace the word pick by reword. This is a way of telling Git that we want to include that commit but its message should be changed.

  • Now when you exit that text editor, Git will open another editor where you can write the new message for your commit.

  • When you are done updating the message exit the editor and the message for the commit will be altered.

Let's take a look at a practical example.

  • We currently have three commits on our master branch and we want to change the second last commit.

Checking the commit messages before updating them

  • We will run the Git Rebase command to alter this commit. We can use the hash of the parent of this commit or we can use HEAD~2.

Running the Git Rebase in the interactive mode

  • Our configured text editor will open and we can see all the commits that were made after HEAD~2.

Viewing the commits in the text editor

  • Next, we will replace pick with reword and close the editor.

Changing pick with reword to change the commit message

  • Another editor will open and there we can change the message.

Updating the message in the new text editor

  • Now when we run the Git Log command we can see that the commit message was updated. Also, the hash is different, indicating that a new commit was created and replaced our existing one.

Viewing the updated commit using git log command

The interactive mode of rebasing can also be used to change multiple commit messages at once. Just replace the word pick by reword for all the commits whose message you want to change and Git will open a text editor for each commit so that you can change the message.

Summary

We frequently make commits in our repository but sometimes, we may want to change the message of a commit. If this commit is the most recent one, then we can use the --amend option with the Git Commit command to update the message.

For older commits, we use the Git Rebase command in the interactive mode. Both of these methods will create a new commit with the updated message and this new commit will replace the older one.

A thing to note here is that Git will block us from pushing this updated commit history to the remote repository because we have effectively altered the history of our project and it will not match the existing history on the remote repository. We can still push by using the -f flag, but make sure that all collaborators are aware of this change. By using the -f flag the history of the remote repository will be overwritten by our local history.