Daily Archives: March 1, 2016

Git cherry-pick a sequence of commits from one branch to another

I finally figured out how to pull a sequence of commits from one branch to another using git’s cherry-pick feature.

In Sakai, when we are preparing for a release, we make a release branch for the release (11.x) and then continue to move master forward and cherry-pick from master back to the release branch. We looked at git-flow and decided against it because most of our adopters use source and not binary artifacts so our release branches live 4-5 years in production we cannot have master “jumping versions”.

So the question is how to cherry-pick back a set of commits to a folder from the baster to the release branch. This is how I do it. First go into master and go into the folder of interest.

git checkout master
cd basiclti
git log .

Capture the log until you find the commit that made the branch.

commit 8cc25781d632e48bfae65009b57c6391d074a3d0
Author: Charles Severance
Date: Mon Feb 29 23:03:28 2016 -0500

SAK-30418 - Initial commit of IMS Content Item

commit 791b12634164003b7c1a59747c28ec9896fc0885
Author: Charles Severance
Date: Sun Feb 28 23:26:51 2016 -0500

SAK-30372 - Fix small mistake in the CASA output

commit 13d21ccd26901c5186a709be27ede499d7de65fc
Author: Charles Severance
Date: Sat Feb 27 11:27:12 2016 -0500

SAK-30372 - Switch the implementation to Jackson
...

Then I cut and paste the entries in reverse order and make a shell script by changing some bits to a comment and changing the commits to “git cherry-pick” – the script ends up as follows:

# To revert, if some cherry-picks go bad
# git checkout 11.x (to be extra sure)
# git reset --hard 11.x

# After all is verified
# git push origin 11.x

# Make sure to be in 11.x first
git checkout 11.x

git cherry-pick aff5c0343b419fda125d9c217d340bb660929c3c
# Author: Charles Severance
# Date: Fri Feb 19 09:49:23 2016 -0500
# SAK-30308 - Change the groupid and artifact id back

git cherry-pick b6acdbee2bd9fd55f8a77de56732582a7eaa08ae
# Author: Charles Severance
# Date: Tue Feb 23 16:17:14 2016 -0500
# SAK-30362 - Fix small issues.


...

Again – the script is the commits in reverse order so you are cherry-picking from oldest to newest. I leave the commit details in as comment for my own sanity.

I like the script in case you need to run this more than once.

Hope this helps someone.