How to Modify Existing, Unpushed Commit Messages?

Better Stack Team
Updated on June 24, 2024

To modify existing, unpushed commit messages in Git, you can use the git commit --amend command. Here's how you can do it:

Step 1: Make Your Changes

First, make sure you're in the branch containing the commit message you want to modify. Then, make the desired changes to your files.

Step 2: Amend the Last Commit

Once you've made your changes, use the following command to amend the last commit:

 
git commit --amend

This will open your default text editor, allowing you to modify the commit message. You can make your changes to the message, save, and close the editor.

Step 3: Push the Changes (If Necessary)

If you've already pushed the original commit to a remote repository, keep in mind that you've rewritten the commit history by amending the commit message. In this case, you'll need to force-push the changes to update the remote repository:

 
git push --force

Note:

  • Be cautious when amending commit messages, especially if you've already shared the commit with others. Rewriting commit history can cause issues for collaborators who have already pulled the original commits.
  • If you've made changes to the commit other than just the message, those changes will also be included in the amended commit.
  • If you want to change a commit message that's not the most recent one, you can use interactive rebase (git rebase -i) to modify older commits.