Cherry Picking a range of commits in Git

Git’s cherry-pick command allows you to specify a range of commits to be cherry-picked onto the current branch.

This can be done with the A123..B234 style syntax – where A123 is the oldest commit in the range.

Consider a scenario with the following chain of commits on my-feature-branch:

commit A123
Author: Troy McCall <troy@atenderrobot.com>
Date:   Thu Aug 9 11:36:54 2018 -0400

- update CARoot

commit B234
Author: Troy McCall <troy@atenderrobot.com>
Date:   Wed Jul 25 15:20:10 2018 -0400

-  Allow wildcards and block heading and trailing dots

commit C345
[....]

commit D456
[....]

commit E567
Author: Troy McCall <troy@atenderrobot.com>
Date:   Thu Jul 19 14:39:25 2018 -0400

- Document Go 1.10 requirement

commit F789
[....]

But you only need commits B234 through D456 , what do ya do?

#take these steps:

  • Open a terminal window in your my-feature-branch
  • run $ git log or open the my-feature-branch commit history on your remote (github) - you’ll need this for reference
  • in another terminal window, $ git checkout master
  • branch off of master $ git checkout -b cherry-pick-my-feature-branch
  • now here’s the tricky part, if you try to run
$ git cherry-pick B234..D456 

This will cherry-pick commits C345 and D456 onto HEAD and skip over B234. This is because the lower-bound is exclusive.

If you want to include B234, run:

$ git cherry-pick B234^..D456 

now push up that bad boy to origin

$ git push -u origin cherry-pick-my-feature-branch

See man git-cherry-pick for more details.

Show Comments