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.
Prerequisites
- Git installed (see our tutorials for installing Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
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.
Note: See how to undo the last commit using git revert.
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:
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:
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"
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:
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.
Note: Learn the difference between Git rebase and Git merge.
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
4. Replace pick
with reword
before each commit message you want to change. For example:
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.
7. After you save the changes for each file you marked for rewording, Git states that the rebase has been successfully completed:
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:
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.