Skip to content

Instantly share code, notes, and snippets.

@odrotbohm
Created July 31, 2012 09:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save odrotbohm/3215345 to your computer and use it in GitHub Desktop.
Save odrotbohm/3215345 to your computer and use it in GitHub Desktop.
Git aliases to ease cherry picking

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:

  1. Looking up the SHA1 of the original commit
  2. Checking out the destination branch
  3. 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!

@tarjoilija
Copy link

I'm not sure what version of Git you are running but according to man pages if you supply cherry-pick a branch name it'll take the first commit only.

(master) $ git commit -m "modified some files"
(master) $ git checkout 1.0.x
(1.0.x) $ git cherry-pick master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment