How to Change Git Commit Message

July 11, 2023

Introduction

Git is a distributed version control system that allows developers to track and manage changes in their codebases. However, as development progresses, sometimes commit messages may have typos, unclear wording, or missing information. In such cases, these messages must be changed to provide a clearer and more accurate description.

In this article, we will show you how to change Git commit messages in a local or remote repository.

How to change a Git commit message - a tutorial.

Prerequisites

How to Change the Most Recent Commit Message

A commit message is a brief description or comment that users provide when making a commit in Git. The purpose of the message is to communicate the intention behind a commit to other developers or yourself in the future.

Changing a commit message also changes the commit ID (the unique SHA1 checksum assigned to each commit). That occurs because changing the commit message creates a new commit that replaces the old one.

To modify the most recent Git commit message, use the git commit --amend command. The command allows you to combine the staged changes with the previous commit instead of creating a new commit. Additionally, you can edit the previous commit message without changing its snapshot.

The steps for changing the latest commit message differ depending on whether the commit has already been pushed to the remote repository.

How to Change Commit Message Before Push

If the commit exists only in the local repository, running the git commit command with --amend changes the commit message. Add the -m option to pass the new message directly from the command line without opening a text editor to enter the new message.

Follow the steps below:

1. Open the terminal or Git Bash.

2. Navigate to the repository directory using the cd command.

3. Use the following syntax to change the commit message:

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

Replace "New commit message" with the message you want the commit to reflect.

For example:

Changing the latest Git commit message.

The command changes the commit message for the latest commit.

If you omit the -m option, Git opens the default text editor and prompts you to enter a new commit message.

For example:

Changing the Git commit message using a text editor.

How to Change Commit Message After Push

Commits that have already been pushed to a remote repository require a force push after amending the message. A force push overwrites the remote repository's branch history with your local branch history. The remote repository is forced to accept your local branch's history as the authoritative one, even if there are differences between them.

Important: Be careful when using force push, as it changes your repository's history. That means that other contributors, who have already cloned your repository, will have to fix their local history manually.

Follow the steps below to change a commit message after it has been pushed to a remote repository:

1. Open the terminal or Git Bash.

2. Use the cd command to navigate to the repository directory.

3. Change the commit message using the following syntax:

git commit --amend -m "New commit message"
Changing a Git commit message directly from the terminal, without opening a text editor.

4. Use the following syntax to force push the new commit to the repository and replace the old commit:

git push --force-with-lease [remote_repository]

For example:

Force pushing a Git commit message change to a remote repository.

The command performs a forced update and overwrites the old commit with the new one in the remote repository.

Note: The git push --force-with-lease command is a safer alternative to git push --force because it first verifies that the remote branch you're pushing to hasn't been updated since your last local fetch. If there have been updates, Git refuses the force push to ensure you don't accidentally overwrite someone else's work.

How to Change Older Commit Messages

While the --amend flag changes only the latest commit message, Git offers a way to change the messages of multiple commits or an older commit through interactive rebasing.

Interactive rebasing is an advanced feature that allows users to modify and rearrange commits during the rebase process selectively. It is a convenient way to edit the commit history by combining, reordering, editing, or deleting commits.

Follow the steps below:

1. Open the terminal or Git Bash.

2. Use cd to navigate to the repository directory.

3. Use the following syntax to display a list of commits with the default text editor:

git rebase -i HEAD~n

Replace n with the number of commits to amend. For example, the following command displays a list of the last five commits on the current branch:

git rebase -i HEAD~5
Listing the latest five commit messages in Git.

4. Replace pick with reword before each commit message you want to change. For example:

Changing multiple Git commit messages through interactive rebase.

In the screenshot above, we replaced pick with reword for the last two commits to change the commit messages for them.

5. Save and close the commit list file. If your default text editor is vi/vim, save the changes and exit the text editor by running:

:wq!

6. For each commit message you marked for rewording, Git prompts you to type the new commit message. Enter the new message, save the file, and close it.

Create a new commit message in a text editor in Git.

7. After you save the changes for each file you marked for rewording, Git states that the rebase has been successfully completed:

Git rebase completed after amending commit messages.

8. When you finish making changes, push the changes to GitHub using the push --force-with-lease command to overwrite the old commits:

git push --force-with-lease [remote_name]

For example:

Force pushing changes to a remote repository in Git.

The command pushes the changes to the remote repository and creates a new commit with a new ID. In addition, every commit that follows the amended commit also gets a new ID because each commit contains the ID of its parent commit.

Any subsequent commits that were originally based on the amended commit get a new ID in order to reference the new commit instead of the old one that was amended.

Note: If your commit message contains sensitive information, force pushing a commit with an amended message may not remove the original commit from GitHub. The original commit could still be cached on GitHub and accessible through its commit ID. To purge the old commit from the remote repository, contact GitHub support and provide the old commit ID.

Conclusion

This article has explained different ways to change a Git commit message in a local and remote repository. Whether you have already pushed the commit to a remote repository or it remains local, Git provides the flexibility to correct any errors or offers a more detailed or descriptive message for the commit(s) in question.

Next, see how to merge the master branch into another one, or read our article on using SSH and HTTPS for Git and decide which security option suits your needs better.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Pull All Branches in Git
March 16, 2023

Learn how to pull all branches from a remote repository into a local one. See examples and download a repository in a few steps.
Read more
Git Tag: An Overview of the Basic Functions
September 6, 2022

This guide shows an overview of basic functions to perform with Git tags. See how to create, delete, push, replace, or checkout a Git tag.
Read more
How to Use Git Stash
September 13, 2022

Git stashes store unfinished work locally, without having to synchronize the changes with a remote repository. See how to create and use Git stash.
Read more
How to Remove Untracked Git Files and Folders
March 8, 2023

Untracked files and folders in a Git repository can often clutter the project. Learn to remove untracked files and folders in Git.
Read more