Data report"State of code review 2024" is now liveRead the full report

How to edit commit messages in Git

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


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.

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.

Terminal
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.

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.

  1. First, start an interactive rebase for the last n commits:
Terminal
git rebase -i HEAD~n

Replace n with the number of commits back you wish to edit.

  1. Git will open a text editor listing the last n commits:
Terminal
pick e3a1b35 Initial commit
pick 7ac9a67 Added new feature
pick 1d2a3f4 Fixed bug
  1. Change pick to reword before the commits whose messages you want to change, then save and close the editor:
Terminal
pick e3a1b35 Initial commit
reword 7ac9a67 Added new feature
reword 1d2a3f4 Fixed bug
  1. 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 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:

Terminal
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:

Terminal
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.

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.

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.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2