153

I want to modify a commit message deeper in history and I've pushed many new commits.

How do I change the commit message? Is it possible?

Mehdi Raash
  • 6,893
  • 1
  • 26
  • 40

7 Answers7

126

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".

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • good answer. Do you know if you are now able to change already pushed commit messages in newer versions of git? Has anything changed since this was posted in '09? – David West Feb 05 '14 at 15:09
  • @DavidWest the same principle holds: you can rewrite your history and force a push. – VonC Feb 05 '14 at 15:10
  • 2
    To make things more specific, if you amend/rebase commits, their commit identifiers (hexadecimal hashes in git index) inevitably changes; it means the edited commits are treated differently from their old commits in git VCS history. That's said, if your dev team members unfortunately already pulled the old commits, they are obliged to pull the edited, new commits, and perform a merge between the old and new in their local working copies. – Shigerello May 09 '14 at 02:00
  • 1
    It's better to push edited commits anew for convenience for your colleagues, thus favorably removing the need of merge in colleagues' working copies. – Shigerello May 09 '14 at 02:09
28

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.

Johan
  • 281
  • 3
  • 2
  • 11
    @Jonah : I'm getting a "Everything up-to-date" message when I try to push to the remote branch – Simon Kagwi Nov 07 '12 at 03:28
  • 1
    As mentioned in another answer : use rebase -i with reword. And it will rewrite the history. – Sylvain Feb 19 '14 at 09:10
  • Thank you for the solution, which I was looking for. You save my time! – Tomasz Kuter Sep 05 '14 at 17:47
  • 1
    @Jonah - I have a problem... your solution updated my commits logs locally, but not remotely. How to push them there? – Tomasz Kuter Sep 05 '14 at 17:52
  • 1
    @TomaszKuter, I had the same problem as you. My commit message wasn't updated remotely. I solved it by using the following help from GitHub: https://help.github.com/articles/changing-a-commit-message. Follow the: Amending the message of older or multiple commit messages section. It is basically the answer from below given by user987419 If you already changed the commit message, you can do the pick and save without having to change it again. – evaldeslacasa Nov 04 '15 at 19:51
  • I do this but when I saw on the bitbucket repository. It wasn't modified the commit message and SHA. I try to git pull or git push. But not work's. What I need to do? can anyone help me? – rld May 16 '17 at 13:49
  • I've tried to use this and also came across the issue of pushing. I believe posting indicates how https://stackoverflow.com/questions/30828957/how-can-i-push-git-replace-to-a-remote-repo. One caveat which is cited in a link in that SO posting is that it will change all descendent hashes. I noticed in a small test repo I made all hashes got changed. So it would make me a bit wary of actually using this method. – Mobile Ben Mar 17 '19 at 23:22
  • push force does not work for me. Local branch showing outdated logs still. – dia Aug 17 '20 at 19:14
19

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!

Nick F
  • 8,686
  • 5
  • 66
  • 84
Marcus
  • 300
  • 2
  • 4
  • 1
    This doesn't allow one to edit messages on merge commits. Is that possible with some variant of this command? – Andrew Mao May 05 '14 at 22:11
  • 1
    Try the `-p` argument of `rebase` which `p`reserves merges. – Cactus Feb 06 '15 at 04:54
  • 3
    I like this procedure, but didn't quite understand the answer at first. In case that someone needs help with it, the Githulb Help page offers good information about it: https://help.github.com/articles/changing-a-commit-message/ – evaldeslacasa Nov 04 '15 at 19:46
16

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.

Huy Vo
  • 2,171
  • 5
  • 23
  • 39
5

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...

Christian Goetze
  • 1,864
  • 2
  • 26
  • 46
2

(From http://git.or.cz/gitwiki/GitTips#head-9f87cd21bcdf081a61c29985604ff4be35a5e6c0)

How to change commits deeper in history

Since history in Git is immutable, fixing anything but the most recent commit (commit which is not branch head) requires that the history is rewritten from the changed commit and forward.

You can use StGIT for that, initialize branch if necessary, uncommitting up to the commit you want to change, pop to it if necessary, make a change then refresh patch (with -e option if you want to correct commit message), then push everything and stg commit.

Or you can use rebase to do that. Create new temporary branch, rewind it to the commit you want to change using git reset --hard, change that commit (it would be top of current head), then rebase branch on top of changed commit, using git rebase --onto .

Or you can use git rebase --interactive, which allows various modifications like patch re-ordering, collapsing, ...

I think that should answer your question. However, note that if you have pushed code to a remote repository and people have pulled from it, then this is going to mess up their code histories, as well as the work they've done. So do it carefully.

Community
  • 1
  • 1
sykora
  • 81,994
  • 11
  • 60
  • 70
  • Good answer in theory, mighty dangerous in practice: see http://stackoverflow.com/questions/253055#432518 – VonC Jan 19 '09 at 12:48
0

If you are using Git Extensions: go into the Commit screen, there should be a checkbox that says "Amend Commit" at the bottom, as can be seen below:

enter image description here

batsheva
  • 1,481
  • 14
  • 23