How to Change Commit Message Using Git Amend

Change Commit Message in Git

Sometimes, you might need to change a commit message if it contains errors, such as typos, incorrect information, or missing details that are important for understanding the changes made in that commit. For example, if a commit message incorrectly describes the changes or lacks crucial information about why the changes were made, updating it can improve clarity and maintain the accuracy of the project’s history. This makes it easier for collaborators to understand the purpose and context of the commit.

In this tutorial, you’ll learn how to change a commit message in Git using the git commit –amend command.

There’s a lot to cover, so let’s get started!

What is Git Amend?

The git commit –amend command allows you modify the most recent commit. For example, changing the commit message, adding new changes, or even removing changes. It’s handy when you notice a typo or forget to include important information. However, be cautious. Amending a commit changes its history, which can cause issues if the commit has already been shared with others.

Changing the Most Recent Commit

Before amending a commit, you must identify the commit that needs modification. You can view your commit history using the git log command.

 # git log

This command lists recent commits, their messages, author information, and commit hashes.

commit 1a2b3c4d5e6f7g8h9i0j (HEAD -> main)
Author: Your Name [email protected]
Date:   Thu Jul 1 14:12:34 2024 +0000

    Initial commit with a typo

To change the message of your most recent commit, run the git commit –amend command:

 # git commit --amend

This command will open the default text editor with the existing commit message. Change it to something like:

    Initial commit with the correct message

Save and close the editor. You will see the following output.

[main 1a2b3c4] Initial commit with the correct message
 Date: Thu Jul 1 14:12:34 2024 +0000
 1 file changed, 1 insertion(+)

This updates the commit message while keeping the changes intact.

Reviewing Changes Before Amending

Before amending, it’s a good practice to review the changes in the commit to ensure you’re modifying the correct one. Use the git show command:

 # git show HEAD

The above command shows the changes made in the most recent commit.

commit 1a2b3c4d5e6f7g8h9i0j (HEAD -> main)
Author: Your Name [email protected]
Date:   Thu Jul 1 14:12:34 2024 +0000

    Initial commit with the correct message

diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..e69de29

Amending the Commit Message Without Opening an Editor

If you prefer to amend the commit message without opening an editor, you can use the -m option followed by the new message:

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

Output:

[main 1a2b3c4] New commit message
 Date: Thu Jul 1 14:12:34 2024 +0000
 1 file changed, 1 insertion(+)

This method is faster and more convenient when you already know the exact message you want to use.

Pushing the Amended Commit

Once you’ve amended the commit, you must push the changes to the remote repository. Since amending a commit changes its hash, you must use the –force option to push the changes:

 # git push --force origin branch-name 

Output:

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 321 bytes | 321.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To https://github.com/yourusername/your-repo.git
 + 1a2b3c4d5e6f7g8h9i0j...1a2b3c4d5e6f7g8h9i0j HEAD -> main (forced update)

Note: Be cautious with force pushing, especially in shared repositories, as it can overwrite commits in the remote repository.

Changing an Older or Multiple Commits

To change an older commit or multiple commits, you can use Git’s interactive rebase feature.

Start an interactive rebase for the last 2 commits:

 # git rebase -i HEAD~2

Output.

pick 1a2b3c4 Initial commit with a typo
pick 5f6g7h8 Another commit message

# Rebase 9f8e7d6..5f6g7h8 onto 9f8e7d6 (2 commands)
#
# Commands:
# p, pick  = use commit
# r, reword  = use the commit, but edit the commit message
# 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
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop  = remove commit
# l, label 

In the above editor, change the pick to reword for the commits you want to amend. Then, save the changes and close the editor.

For each selected commit, a new text editor window will open. Change the commit message, save the file, and close the editor.

Push the changes to the remote repository with force:

 # git push --force origin branch-name

Conclusion

In this article, you’ve learned to change a commit message using simple commands like git commit –amend for the latest commit or interactive rebase for older commits. Remember to communicate with your team when rewriting public commits to avoid confusion.

FAQs

1. Can I amend a commit that has already been pushed?

Yes, but you need to force-push the amended commit, which can overwrite changes in the remote repository. Be cautious when doing this in shared projects.

2. What happens if I amend a commit without pushing?

If you amend a commit but don't push it, the changes are only local. You can still revert or modify the commit before pushing it.

3. How do I revert an amended commit?

You can use git reflog to find the original commit and reset your branch to that commit using git reset --hard .

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva