Search code examples
gitgit-cherry-pick

Git cherry-pick range


How can I cherry-pick commit C4 and C5?

GitTree

I’ve tried git cherry-pick C4..C5 but I only get C4. I think I don’t really understand how this range thing works.


Solution

  • TL;DR version: C4^..C5

    Unless a command treats it specially (and git cherry-pick does not), a range expression means "take all commits identified/findable by the right-side name, minus all commits identified/findable by the left-side name". In this case C5 identifies commits 0 through 5 inclusive, and C4 identifies 0 through 4 inclusive. Subtracting the second set of commits from the first set leaves you with just C5.

    Another way to look at it is that if the commits are related, you get a "half open interval", in the same way that the integers in (3, 7] are 4 5 6 7.

    As with the half-open integer interval, the usual easy trick is to start from one step back. Since the (first) parent of C4 is C4^, C4^..C5 does the trick, just as (2, 7] would get you 3 4 5 6 7.

    (This fails if there's no parent, i.e., the left side is the root commit. In that case you need some alternative strategy. Some commands make this easier, some make it harder, but for now we can just ignore the problem. :-) )