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?
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?
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. Take care when rebasing merges; you’ll need to use -r (--rebase-merges), and read the “Rebasing merges” section of the git rebase manpage.
To quickly edit only the last commit, run
git commit --amend
(but beware of anything already staged for commit).
preserve-merges)
– D. Ben Knoble
Dec 04 '18 at 20:07
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
commit --amend”, unless you mess up the committing process somehow.
– Stephen Kitt
Dec 04 '18 at 15:56
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 04 '18 at 15:57
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 04 '18 at 16:17