11

For lazy reasons I pushed a bunch of commits with default messages and now it has become cumbersome, as I don't really know what I've changed in each commit.

How do I edit just the messages of previous commits and (if possible) keep the commit tree?

  • 10
    Beware of changing public history! – D. Ben Knoble Dec 4 '18 at 20:07
  • 2
    i fear the answers here aren't giving you nearly dire enough warnings. this will create a huge mess if anyone else has pulled your history in the meantime — all the moreso if they've committed new work on top of it! – Eevee Dec 5 '18 at 6:15
  • I'll use it with cause, I have simpler case here, I'm the only that use this repo. – Tuyen Pham Dec 5 '18 at 6:20
  • 3
    Info: This is already asked (possibly many times) on Stack Overflow. – user202729 Dec 5 '18 at 10:05
20

To edit the commit messages of a series of commits, I run

git rebase -i firstsha

where firstsha is an identifier for the parent commit of the first commit I want to edit. (You can use any valid reference here, so git rebase -i HEAD~4 will show the last four commits.)

In the editor that opens, change all the “pick” entries to “reword” on commits you wish to modify, then close the editor; you will then be asked to enter commit messages for all the commits you chose.

Note that this will change the commit tree, because the hashes of the commits will change. You will have to force-push your new tree, or push it to a new branch. It will also mess up merges, so avoid editing merge commits.

To quickly edit only the last commit, run

git commit --amend

(but beware of anything staged for commit though).

  • How would it affect commit tree, will it create an intermediate vertical line in commit tree? I had only one vertical line commit tree now, and over 100 messages need to be changed. – Tuyen Pham Dec 4 '18 at 15:54
  • 3
    It won’t create a new “line” in your commit tree, it will change all the commits. If your history is currently linear, it will remain linear. – Stephen Kitt Dec 4 '18 at 15:55
  • It depends. Are you using it by yourself and not pushing to a server? Then no, only one "line". But otherwise there might be two, and based on your answers to the first two we can help you get rid of the old "line". – Captain Man Dec 4 '18 at 19:50
  • 1
    Rebase does have a flag to handle merges (I think it’s preserve-merges) – D. Ben Knoble Dec 4 '18 at 20:07
6

What you are looking for is git rebase.

If you only wish to change the previous git commit message then you only need to use the following:

git commit --amend

And make the changes you desire to the previous commit and then save the edits.

However if you need to change older commits you need to use rebase.

git rebase -i HEAD~N 

where N equals the number of commits you wish to go back to, e.g 2 or 12 or 6, etc. etc.

Here you should get a text editor with your commits. Change the option from pick to reword to change the message.

Once you have identified all the commits you wish to change and have appropriately changed their options, save and close the editor. Then make the changes to each commit message. Once you are satisfied you can run:

git push --force

And you should have maintained your git history albeit with different hash values because you have made the necessary changes you wish. Here are some additional links you should check out:

7.6 Git Tools - Rewriting History
GitHub Help - Changing a Commit Message
StackOverflow - Question on Changing old commit messages

  • If you “reword”, you don’t need to “commit --amend”, unless you mess up the committing process somehow. – Stephen Kitt Dec 4 '18 at 15:56
  • Thanks, here the steps that I'll do as my understanding from your post: 1. do git rebase -i firstsha that firstsha is parent commit's hash of the commit that I'd want to change the message, then in editor, change pick to reword, enter new message, then issue git rebase --continue and do git push --force? – Tuyen Pham Dec 4 '18 at 15:57
  • @StephenKitt, I just noticed your answer, I was making mine as you submitted yours. I will follow your advice and look to make those edits. If it is more appropriate I can merge my answer into yours by adding the links to yours if you feel that would better suit this question than having multiple similar answers. – kemotep Dec 4 '18 at 16:03
  • @TuyenPham you only need to do git rebase -i HEAD~N with N being the number of commits back you wish to go. Change every commit option that you want to edit the message of from pick to reword, save this file, make the changes to each of those commit files and save those. Once you are confident you are done you only need git push --force [Name of git branch you are were working on]. You can always go back and do this again or do it in stages. – kemotep Dec 4 '18 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.