How do I edit an incorrect commit message in git ( that I’ve pushed )?

The message from Linus Torvalds may answer your question:

Modify/edit old commit messages

Short answer: you can not (if pushed).


extract (Linus refers to BitKeeper as BK):

Side note, just out of historical interest: in BK you could.

And if you’re used to it (like I was) it was really quite practical. I
would apply a patch-bomb from Andrew, notice something was wrong, and just
edit it before pushing it out.

I could have done the same with git. It would have been easy enough to
make just the commit message not be part of the name, and still guarantee
that the history was untouched, and allow the “fix up comments later”
thing.

But I didn’t.

Part of it is purely “internal consistency”. Git is simply a cleaner
system thanks to everything being SHA1-protected, and all objects being
treated the same, regardless of object type. Yeah, there are four
different kinds of objects, and they are all really different, and they
can’t be used in the same way, but at the same time, even if their
encoding might be different on disk, conceptually they all work exactly
the same.

But internal consistency isn’t really an excuse for being inflexible, and
clearly it would be very flexible if we could just fix up mistakes after
they happen. So that’s not a really strong argument.

The real reason git doesn’t allow you to change the commit message ends
up being very simple: that way, you can trust the messages. If you allowed
people to change them afterwards, the messages are inherently not very
trustworthy.


To be complete, you could rewrite your local commit history in order to reflect what you want, as suggested by sykora (with some rebase and reset –hard, gasp!)

However, once you publish your revised history again (with a git push origin +master:master, the + sign forcing the push to occur, even if it doesn’t result in a “fast-forward” commit)… you might get into some trouble.

Extract from this other SO question:

I actually once pushed with –force to git.git repository and got scolded by Linus BIG TIME. It will create a lot of problems for other people. A simple answer is “don’t do it”.

Leave a Comment