Zocoi Vietnamese /ʒoʊ kɔɪ/: (informal) Let's watch/read together

Amend a git commit message from you in the past

Today my feature branch could not be merged to master because one my commit message did not meet the merging requirements. This is how I dived into a haystack, found the commit, rebased and upated it.

First, let's find all the commits made by me 2 days ago

[code]
git log --since=2.days --author=hung
[/code]

[code]
commit 5e18ea5bd6b92d2382a545b07e3495c693fa6293
Author: Hung Dao
Date: Fri Nov 16 12:15:55 2012 -0800

sample commit message 1

commit d3727aafbadbd7befde0dd497afec3427f0bf151

Author: Hung Dao
Date: Mon Dec 3 12:57:12 2012 -0800

sample commit message 2

...
[/code]

Git doesn’t have a tool to modify history, so in order to modify a commit that is farther back in my history, there is the rebase tool to rebase a series of commits onto the HEAD. I must also define how far I want to go back and rewrite history. For this case, I want to go 1 step further than the culprit commit

[code]
git rebase -i d3727aaf~1

edit d3727aa change the commit message
pick ....

# Rebase dcfa286..5e18ea5 onto dcfa286
#
# Commands:
# p, pick = use commit
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Successfully rebased and updated ...
[/code]

Since the commits are listed in oposite order compared to chronological order I see in `git log`, the commit I want to change should be the first one. After rebasing, let's run `git log` one more time to confirm the change was there.

[code]
git log --since=2.days --author=hung
[/code]

And voilà!

Warning: Since it was only me who's working on the branch so I can do `git push --force origin `. It is strongly recommended not to rewrite history of a collaborative branch or repo since it may throw away other people's commits.