Posted on Thu Apr 20 2023

Rewriting History: How to Change Commit Messages for Older Pushed Commits

Rewriting History: How to Change Commit Messages for Older Pushed Commits

Today, after pushing a feature branch to the remote repository, the tech lead returned my ticket because I didn't add the ticket's prefix in the commit message. I had already pushed 3 commits and thought it was impossible or too complicated to fix without ruining the project. However, little did I know about Git's deep stuff.

In this situation, you have two options: update the most recent commit or update a certain number of commits.

Update the most recent commit

To update the most recent commit message, you can use the --amend flag:

git commit --amend

This command will open a text editor with the commit message of the most recent commit. You can modify the message as desired and save the file. Git will then create a new commit object with the same parent as the most recent commit, but with the updated commit message. Keep in mind that amending a commit changes its SHA-1 hash, which means it's essentially creating a new commit that replaces the old one. If the commit is already pushed into a remote repository, you need to push with the force flag. Or as a safer way, run this:

git push --force-with-lease

Learn more about git push --force-with-lease vs. --force

Updating older commit messages

Although it's generally not recommended to modify the Git history of a shared repository, sometimes it's necessary to modify an older commit message. To do so, you can use Git's rebase command with the -i flag to launch an interactive rebase session. In the interactive rebase session, you can modify the commit message for each commit as desired.

For example, let's say you want to modify the commit message for the third oldest commit in your repository. You can run the following command to start an interactive rebase session:

git rebase -i HEAD~3

Not sure saraha This command will launch an interactive rebase session for the last three commits in your Git history. In the text editor that opens, locate the commit you want to modify and change the first word from pick to reword. This tells Git that you want to modify the commit message for this commit. You can modify multiple commits.

pick e499d89 this is the most recent commit
pick 0c39034 add feature commit
pick f7fde4a I did a mikstake in this comit, oops

Save and close the rebase file. For each commit you have reworded, Git will prompt you to modify the commit message in a new text editor session. Modify the message as desired, save and close the file. Git will then continue with the interactive rebase session and apply your changes.

Keep in mind that modifying older commit messages in this way changes the Git history, which can cause issues if you have already pushed the commits to a remote repository. This is because Git uses the SHA-1 hash of the commit message as a unique identifier for each commit. If you change the message, the SHA-1 hash will also change, which means the modified commit is a new, different commit from the original. so if the commits are already pushed you need to force the push

git push --force


In conclusion, while modifying commit messages is generally not recommended, there are situations where it becomes necessary. Git's --amend flag and interactive rebase session with the reword command can be used to update the most recent commit or a certain number of commits' messages, respectively. It's important to note that modifying commit messages can change the Git history and cause issues if the commits have already been pushed to a remote repository. Therefore, it's crucial to communicate with your team and consider the potential consequences before making any changes.