Sunday, July 17, 2011

Fixing Git Commit Messages

Problem: Commits in git are immutable, so how do you recover if you make a mistake when you commit? For example, I forgot to put my task number, "ITSM-197" at the beginning of my commit message. I want to fix this before pushing to a remote repo.

btaylor@vancouver ~/src/change-service (ITSM-197) $ git log --oneline master..
87b9f6b fix response elements outside of method element
3123405 ITSM-197 change approver to approval-level
56ecbb4 ITSM-197 remove duplicate declaration of status attribute
7f10503 ITSM-197 fix change request state machine representation in xsd


Whoops, commit 87b9f6b didn't follow the convention. I fix this with "git commit --amend –m" which replaces the tip of the current branch with a new modified commit. You can leave the –m option off to actually change the commit itself (and keep the existing message).

btaylor@vancouver ~/src/change-service (ITSM-197) $  git commit --amend -m "ITSM-197 fix response elements outside of method element"
[ITSM-197 c7310f8] ITSM-197 fix response elements outside of method element
 1 files changed, 6 insertions(+), 6 deletions(-)

btaylor@vancouver ~/src/change-service (ITSM-197) $ git log --oneline master..
c7310f8 ITSM-197 fix response elements outside of method element
3123405 ITSM-197 change approver to approval-level
56ecbb4 ITSM-197 remove duplicate declaration of status attribute
7f10503 ITSM-197 fix change request state machine representation in xsd

Solved. Note that the chain of commits from the master branch has been rewritten to end with my new commit c7310f8 with my ITSM-197 message added. Let's verify that these have the same tree:

btaylor@vancouver ~/src/change-service (ITSM-197) $ git cat-file commit c7310f8
tree f1c5c976a169c513800d9cd99a776957f503886e
parent 3123405682b942e4875399c66265caad42260d64
author Bryan Taylor <btaylor@nospam.com> 1310325458 -0500
committer Bryan Taylor <btaylor@nospam.com> 1310395725 -0500

ITSM-197 fix response elements outside of method element
 
btaylor@vancouver ~/src/change-service (ITSM-197) $ git cat-file commit 87b9f6b
tree f1c5c976a169c513800d9cd99a776957f503886e
parent 3123405682b942e4875399c66265caad42260d64
author Bryan Taylor <btaylor@nospam.com> 1310325458 -0500
committer Bryan Taylor <btaylor@nospam.com> 1310325458 -0500

fix response elements outside of method element

Yep, they both have tree f1c5c97. Note that the previous 87b9f6b commit still exists, but it's no longer in the commit chain from master to the tip.


4 comments: