Editing commit messages in Git is useful for correcting typos, adding more context, or modifying the message to follow a project's commit message guidelines. This guide will walk you through the different methods of editing commit messages, whether they are recent, older, stored locally, or already pushed to a remote repository.
Editing the most recent commit message
If you need to edit the message of the most recent commit, you can use the git commit --amend
command. This doesn't actually "edit" the existing commit but rather replaces it with a new commit that has the same parent, effectively rewriting the commit history.
git commit --amend -m "New commit message"
Upon executing this command, Git will replace the latest commit with a new commit that has your updated message, keeping all your code changes intact.
Editing older or multiple commit messages with git rebase
To edit a commit message that is older than the most recent commit, you will need to use git rebase
in interactive mode (-i
). This method allows you to change any commit message in the repository's history.
- First, start an interactive rebase for the last
n
commits:
git rebase -i HEAD~n
Replace n
with the number of commits back you wish to edit.
- Git will open a text editor listing the last
n
commits:
pick e3a1b35 Initial commitpick 7ac9a67 Added new featurepick 1d2a3f4 Fixed bug
- Change
pick
toreword
before the commits whose messages you want to change, then save and close the editor:
pick e3a1b35 Initial commitreword 7ac9a67 Added new featurereword 1d2a3f4 Fixed bug
- Git will then open the editor for each commit you marked for rewording. Change the commit message, save, and close the editor.
After completing these steps, Git will apply the new commit messages to your history.
Editing pushed commit messages
Editing commit messages that have already been pushed to a remote repository is more complex and should be done with caution, as it can affect any other collaborators who has already pulled these changes.
To change a pushed commit message, follow the steps outlined in the previous section to amend or rebase your commits locally. Once you've made your changes, you will need to force push them to the remote repository:
git push --force
This will destructively overwrite the specified commits, and should be used with caution.
As an added precaution, you can use the --force-with-lease flag:
git push --force-with-lease
The --force-with-lease
option is safer as it checks if the remote branch has changed before pushing. This helps prevent you from overwriting a collaborator's changes.
Warning: Force pushing can rewrite history in a way that is disruptive to collaborators. Always communicate with your team before performing such actions, especially on shared branches.
Editing local commit messages
If your commit hasn't been pushed yet, you can safely use the git commit --amend
command for the most recent commit or git rebase -i
for older commits, as described earlier. This will not affect other collaborators since the changes have not been shared.
Git Commit message best practices
While editing commit messages, aim to follow best practices:
- Keep the first line under 50 characters.
- Use the body to explain "what" and "why" rather than "how".
- Use imperative subject lines: "add logging" instead of "added logging".
- Separate the subject from the body with a blank line.
- Follow your organizations commit style, or use conventional commit styling.
For further reading see the official Git documentation.