159

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?

7 Answers 7

129

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

4
  • 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 5, 2014 at 15:09
  • @DavidWest the same principle holds: you can rewrite your history and force a push.
    – VonC
    Feb 5, 2014 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 9, 2014 at 2: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 9, 2014 at 2:09
29

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.

8
  • 12
    @Jonah : I'm getting a "Everything up-to-date" message when I try to push to the remote branch Nov 7, 2012 at 3:28
  • 1
    As mentioned in another answer : use rebase -i with reword. And it will rewrite the history.
    – Sylvain
    Feb 19, 2014 at 9:10
  • Thank you for the solution, which I was looking for. You save my time! Sep 5, 2014 at 17:47
  • 1
    @Jonah - I have a problem... your solution updated my commits logs locally, but not remotely. How to push them there? Sep 5, 2014 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: 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. Nov 4, 2015 at 19:51
21

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!

3
  • 1
    This doesn't allow one to edit messages on merge commits. Is that possible with some variant of this command?
    – Andrew Mao
    May 5, 2014 at 22:11
  • 1
    Try the -p argument of rebase which preserves merges.
    – Cactus
    Feb 6, 2015 at 4:54
  • 4
    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: help.github.com/articles/changing-a-commit-message Nov 4, 2015 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.

0
7

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

1
  • 3
    "git notes" might serve a similar purpose May 20, 2014 at 16:44
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.

1
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

1
  • @KrunalPandya yes, or just press commit & push
    – batsheva
    Oct 6, 2019 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.