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".
Currently a git replace might do the trick.
In detail: Create a temporary work branch
git checkout -b temp
Reset to the commit to replace
git reset --hard <sha1>
Amend the commit with the right message
git commit --amend -m "<right message>"
Replace the old commit with the new one
git replace <old commit sha1> <new commit sha1>
go back to the branch where you were
git checkout <branch>
remove temp branch
git branch -D temp
push
guess
done.
You can use git rebase -i
(against the branch you branched from)
'i' for interactive.
Replace the pick
next to the commit comment you wish to change with r
(or reword
), save and exit and upon doing so you'll be able to make the edit.
git push
once again and you're done!
Suppose you have a tree like this:
dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
First, checkout
a temp branch:
git checkout -b temp
On temp
branch, reset --hard
to a commit that you want to change its message (for example, that commit is 946992
):
git reset --hard 946992
Use amend
to change the message:
git commit --amend -m "<new_message>"
After that the tree will look like this:
dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
\
b886a0 [temp]
Then, cherry-pick
all the commit that is ahead of 946992
from master
to temp
and commit them, use amend
if you want to change their messages as well:
git cherry-pick 9143a9
git commit --amend -m "<new_message>
...
git cherry-pick 5a6057
git commit --amend -m "<new_message>
The tree now looks like this:
dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
\
b886a0 - 41ab2c - 6c2a3s - 7c88c9 [temp]
Now force push the temp branch to remote:
git push --force origin temp:master
The final step, delete branch master
on local, git fetch origin
to pull branch master
from the server, then switch to branch master
and delete branch temp
.
Now both your local and remote will have all the messages updated.
At our shop, I introduced the convention of adding recognizably named annotated tags to commits with incorrect messages, and using the annotation as the replacement.
Even though this doesn't help folks who run casual "git log" commands, it does provide us with a way to fix incorrect bug tracker references in the comments, and all my build and release tools understand the convention.
This is obviously not a generic answer, but it might be something folks can adopt within specific communities. I'm sure if this is used on a larger scale, some sort of porcelain support for it may crop up, eventually...