Live streamJoin us for "State of code review 2024" on July 18thRegister today

How to change a commit message in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


Changing a commit message on Git can be necessary for several reasons, such as correcting spelling errors, adding missing information, or clarifying the purpose of the changes. However, editing commit messages, especially after they have been pushed, should be done with caution as it can affect the repository history. This guide will explain how to change commit messages in your local and remote Git repositories.

If you need to change the message of the most recent commit that has not been pushed to Git, you can use the git commit --amend command. This is the simplest case and does not affect other users.

  1. Open your terminal.

  2. Navigate to your repository.

  3. Run the following command:

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

    This command opens the default text editor set in your configuration, allowing you to change the commit message. Save the changes and close the editor.

Example:

If your last commit message was "Initial commit" and you want to change it to "Add initial project structure", you would use:

Terminal
git commit --amend -m "Add initial project structure"

Changing a commit message after a push modifies the repository's history. If other collaborators have already pulled the changes, this can cause conflicts and disrupt their work.

If you need to change the message of a commit that has already been pushed or is not the latest commit, you will need to perform an interactive rebase:

  1. Determine how many commits back you need to go.

  2. Use the following command to start the rebase:

    Terminal
    git rebase -i HEAD~n

    Replace n with the number of commits you want to review.

  3. In the editor that opens, change pick to reword for the commit you want to change.

  4. Save and close the editor.

  5. Change the commit message in the new editor window that opens.

  6. Save and close the editor to complete the rebase.

Example:

Suppose you want to change a commit message three commits back. You would start with:

Terminal
git rebase -i HEAD~3

In the text editor, find the commit you want to edit, change pick to reword, and then save and exit. When prompted, rewrite the commit message, save, and exit again.

After changing the commit message through rebase, you will need to push the changes to Git. Since the history has changed, you will need to use the --force option:

Terminal
git push --force

Using git push --force can overwrite changes in the remote repository. Communicate with your team before force-pushing to avoid disrupting others' work.

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.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2