When fixing bugs on a software project I repeatedly faced the situation where I'd like to apply the commit I just did to a branch to another. Think of fixing a bug in the master branch and immediately back-porting it to the maintenance branch. This usually needed the following steps:
- Looking up the SHA1 of the original commit
- Checking out the destination branch
git cherry-pick $sha1
The very first step can be eased a bit by creating a git alias to lookup the SHA1 of the last commit of a given branch.
[alias] last = !git log -n 1 --pretty=format:%H $1
This will allow us the following:
$ git last 58f12b8d8fc21b12ad057192128633b466cf918b // SHA1 of the last commit of the current branch $ git last 1.0.x ceec0bcc4ab3bd72895a05eb702f846c6f952cfd // SHA1 of the last commit of the 1.0.x branch
We can now create yet another alias to lookup the last SHA1 of a given branch and pipe that into the
cherry-pick command as follows:
[alias] last = !git log -n 1 --pretty=format:%H $1 cp-last = !git cherry-pick `git last $1`
Immediately back-porting a fix now looks as follows:
master $ git commit -m "$commitmessage" master $ git checkout 1.0.x 1.0.x $ git cp-last master
Feedback and suggestions for improvements welcome!