Oct 18, 2023 4 min read

How to Change a Git Commit Message

Change a Git Commit Message with our step-by-step tutorial. Git is a fast and user-friendly version control system for projects of all sizes.

Change a Git Commit Message
Table of Contents

Before we begin talking about how to change the Git commit message, let’s briefly understand - What is Git?

Git is a popular open-source distributed version control system that can handle a number of things right from small to very large projects with great speed and efficiency. The fact that it is user-friendly and easy to learn makes it stand out.

In this tutorial, you will change the git commit message. We will also address a few FAQs on how to change the git commit message.

Change the Most Recent Commit

You can edit the most recent commit message with the git commit --amend command.

Commit is not pushed

If you want to update the message of a recent commit that hasn't been pushed to the remote repository, use the --amend flag to commit it again.

1) In your terminal, navigate to the repository directory.

2) To update (alter) the message of the most recent commit, run the following command:

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

The command replaces the most recent commit with the newly created one.

You can use the -m option to compose a new message on the command line without having to launch an editing session.

You can even add other modifications you forgot about before altering the commit message:

git add .
git commit --amend -m "New commit message."

Pushed Commit

The new entity with a different SHA-1 is the modified (updated) commit. In the current branch, the preceding commit will no longer exist.

In general, you should avoid changing a commit that has already been pushed because it may cause problems for those who have relied on it for their work. Before altering a pushed commit, it's a good practice to discuss with your other devs.

You'd have to forcibly push the most recently pushed commit if you altered the message.

1) Go to the repository's page.

2) Modify the message in the most recent pushed commit:

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

Force push to update the remote repository's history:

git push --force <remoteName> <branchName>

Change an Older or Multiple Commits

You can use an interactive git rebase to alter the message of one or more older commits if you need to update the message of one or more older commits.

The rebase command rewrites the commit history, hence rebasing commits that have already been published to the remote Git repository is strongly discouraged.

1) Go to the repository where the commit message you want to alter is stored.

2) Type git rebase -i HEAD~N, where N represents the number of commits to rebase. If you wish to alter the 4th and 5th most recent commits, for example, type:

git rebase -i HEAD~5

In your default text editor, the command will show you the most recent X commits:

pick 43f8707f9 fix: update dependency json5 to ^2.1.1
pick cea1fb88a fix: update dependency verdaccio to ^4.3.3
pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2
pick c5e078656 chore: update dependency flow-bin to ^0.109.0
pick 11ce0ab34 fix: Fix spelling.

# Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 commands)

3) Replace pick with reword on the lines of the commit message you want to change:

reword 43f8707f9 fix: update dependency json5 to ^2.1.1
reword cea1fb88a fix: update dependency verdaccio to ^4.3.3
pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2
pick c5e078656 chore: update dependency flow-bin to ^0.109.0
pick 11ce0ab34 fix: Fix spelling.

# Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 commands)

4) Close the editor after saving your changes.

5) A new text editor window will open for each chosen commit. Change the commit message, then save and exit the editor.

fix: update dependency json5 to ^2.1.1

6) Force the modifications to be pushed to the remote repository:

git push --force <remoteName> <branchName>

FAQs to Change a Git Commit Message

Is it possible to update an older commit message? 

Yes, you can use git rebase -i to edit older commits.

What if I want to modify multiple commit messages? 

With git rebase -i, you can choose to edit, squash, or reword multiple commit messages.

Can I change commit messages in a shared branch? 

Yes, but it requires force-pushing, which can cause problems for others. Consult with your team before doing this.

I made a mistake in my Git commit message, what should I do? 

Use git commit --amend to modify the mistake and replace it with a corrected message.

Is there a way to see the commit history and make changes to messages later? 

Yes, you can use git log to view the commit history, and then use git rebase -i to edit the messages.

What if I accidentally amend the wrong commit message? 

You can undo the changes by running git reflog and then resetting to the commit you want.

Are there any restrictions or limitations when modifying Git commit messages? 

Modifying commit messages is generally safe, but be aware that it affects the commit history, which may cause conflicts or confusion if others have already based their work on those commits.

Conclusion

Use the git commit --amend command to update the most recent commit message. Use git rebase -i HEAD~N to update older or multiple commit messages.

Don't make changes to pushed commits because it could cause a lot of headaches for your coworkers.

Leave a comment below if you run into any issues or have any feedback.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.