5 min read
How to change the last commit message after pushed?

Typo in the commit message and no one should see it? Entered any wrong message?

I think it has happened to every developer. You’ve made a mistake when using git. Some mistake in the commit message. The fingers were too fast, the brain was already turned off or the cat fell on the keyboard. You never know, many things can happen.

It’s not a big deal at first. It is only the commit message and does not affect the code at all. But if you’ve searched intensively for important information in a Git history for the first time, you’ll understand that good and especially correct information in the commit messages can be tremendously helpful. Especially when searching commit messages in an automated way, even a typo can break the search.

So what do I learn from this? Just spend 3 seconds longer for the commit message and if I make a mistake, then I correct it. So, how do I do this?

The very first question is: Have you pushed yet?

If you know how git works, then you will understand why this question is relevant. Because if you haven’t pushed to the remote repository yet, then you only have to fix the mistake on your end. The server has not noticed the new commit yet.

Let’s take a look at my little example project. This is the commit history:

❯ git log --oneline
cc20511 (HEAD -> main, origin/main, origin/HEAD) asdf
ee9b5d4 Initial commit

The newest commit has an crazy message “asdf”. I want to change that. The newest commit is not pushed to the remote repository. I can see that, because the origin/main tag ist on my initial commit.

Now, let’s change the latest commit message:

❯ git commit --amend -m "add information to readme"

[main 52605df] add information to readme
 Date: Fri Nov 25 16:01:02 2022 +0100
 1 file changed, 1 insertion(+)


I can see the changes affected my git history:

❯ git log --oneline
52605df (HEAD -> main) add information to readme
ee9b5d4 (origin/main, origin/HEAD) Initial commit


I can now push the new commit to the server. It will arrive there with the modified message and all are happy and satisfied.

Shit, you already pushed? No problem, it’s still solvable.

At this point, it’s a different problem. The incorrect commit message is not only in my local repository, but also in the repository on the server.

Let me recreate the problem with the same repository. It can now be seen that the origin/main and origin/head are sitting on my incorrect “asdf” commit when I look at the git history of my project:

❯ git log --oneline
cc20511 (HEAD -> main, origin/main, origin/HEAD) asdf
ee9b5d4 Initial commit

We can change the latest commit message in our local repository the same way we did before:

❯ git log --oneline
cc20511 (HEAD -> main, origin/main, origin/HEAD) asdf
ee9b5d4 Initial commit

Now we try to push our changes to the server:

❯ git push
To <https://git>.***.de/rklimpel/git-experiments.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to '<https://git>.***.de/rklimpel/git-experiments.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.


Rejected? shit. Why can’t I…? Yeah, because the remote repository and yours are no longer synchronized. And it’s not a solution to just pull it again, because then the old commit that is already on the server will be added to your local repository in addition to your fixed commit.

This will look approximately like this in you history:

❯ git log --oneline
cc20511 (HEAD -> main, origin/main, origin/HEAD) asdf
ee9b5d4 Initial commit

This happens because the commit –amend command does not actually edit the commit, but wraps the same changes in a new commit with the new message and removes the previous one. That’s why you see a different commit hash on the commits that were edited with amend.

So, what’s the solution?

I use a force push. Simply told, I force the git repository on the server to accept the changes from my local repository in case of any differences in the history. And therefore also accept my changed git history with the corrected message.

So the full solution would look like this:

git commit --amend -m "my new commit message"
git push --force

Whenever you use commands like force you should be aware of what you are doing. Keep in mind that you can make quite a mess with it. However, if your correction comes right after your actual commit and no other developer or service has picked up the changes from the remote repository yet, there will be no problem.