Practical Example

Let’s assume you want to change the message of the most recent commit:

  • Run:
   git commit --amend

command process

  • Edit the commit message in the editor that opens.
  • Save and close the editor.
  • If the commit was already pushed, force push it to the remote repository:
   git push origin main --force

For changing an older commit, let’s say the last 3 commit:

  • Run:
   git rebase -i HEAD~3

git rebase -i HEAD~3

  • In the editor, change `pick` to `reword` for the commit(s) you want to change.
  • Save and close the editor.
  • Edit the commit messages as prompted.
  • Force push the rebased commits:
   git push origin main --force

By following these steps, you can effectively manage and correct commit messages in your Git repository. Always communicate with your team when rewriting history to avoid conflicts and data loss.


How to Change Commit Message in Git?

Changing a commit message in Git can be done in a few different ways, depending on whether the commit is the most recent one or an earlier commit. Here’s an article on how to change a commit message in Git, covering scenarios both before and after the commit has been pushed to a remote repository.

These are the following topics that we are going to discuss:

Table of Content

  • Changing the Most Recent Commit Message (Not Pushed Yet)
  • Changing a Commit Message That Has Already Been Pushed
  • Interactive Rebase (For Older or Multiple Commits)
  • Practical Example

Similar Reads

Changing the Most Recent Commit Message (Not Pushed Yet)

If the commit only exists in your local repository and has not been pushed to a remote repository, you can amend the commit message using the following command:...

Changing a Commit Message That Has Already Been Pushed

If you’ve already pushed the commit to a remote branch, you’ll need to force-push the amended commit. Force pushing can overwrite the history on the remote repository, so use it with caution....

Interactive Rebase (For Older or Multiple Commits)

If you need to update messages for multiple commits or older commits, use interactive rebase:...

Practical Example

Let’s assume you want to change the message of the most recent commit:...