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

git log --since=2.days --author=hung
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

...

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

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

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.

git log --since=2.days --author=hung

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s