Data report"State of code review 2024" is now liveRead the full report

How to cherry-pick a range of commits

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Cherry-picking in Git allows you to select specific commits from one branch and apply them to another branch. This can be particularly useful in maintaining multiple branches for different purposes, such as bug fixes, feature development, or experimenting with new ideas without disrupting the main codebase. In this guide, we'll explore how to cherry-pick a range of commits effectively, including selecting multiple commits, excluding specific commits, and applying commits to multiple branches.

Cherry-picking is a powerful Git feature that enables you to choose commit(s) from a branch and apply them onto your current branch. This operation is useful for porting bug fixes and features without merging entire branches, thus maintaining a clean project history.

The basic command for cherry-picking a single commit is:

Terminal
git cherry-pick <commit-hash>

Where <commit-hash> is the unique identifier for the commit you want to cherry-pick.

Stop wrestling with Git commands

The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop Googling Git commands.

Learn more

To cherry-pick a sequence or range of commits, Git provides a straightforward way to specify commit ranges using the .. (two dots) syntax.

The command to cherry-pick a range of commits from commit A to commit B (inclusive) is:

Terminal
git cherry-pick A^..B

Here, A^ means "the parent of A," ensuring that commit A is included in the range. This syntax picks all commits starting just after commit A's parent up to and including commit B.

Suppose you want to cherry-pick the last three commits from another branch called feature. First, check out the branch where you want to apply the commits:

Terminal
git checkout main

Then, find the hashes of the commits you want to cherry-pick (assuming they are the last three commits on feature):

Terminal
git log feature --oneline -3

This will list the last three commits. If the commits have hashes 1234567, 2345678, and 3456789, you cherry-pick them as follows:

Terminal
git cherry-pick 1234567^..3456789

To cherry-pick commits to multiple branches, you need to repeat the cherry-pick process for each branch. For example:

  1. Cherry-pick commits to the first branch:

    Terminal
    git checkout branch1
    git cherry-pick A^..B
  2. Switch to the second branch and repeat:

    Terminal
    git checkout branch2
    git cherry-pick A^..B

To exclude a specific commit from a range, you must break the cherry-picking process into parts that skip the unwanted commit. For example, to cherry-pick from commit A to D but exclude commit C, you can do:

Terminal
git cherry-pick A^..B
git cherry-pick D

Where B is the commit just before C, and D is the commit just after C.

The best engineers use Graphite to simplify Git

Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.

Git started for free
  • Test each commit: After cherry-picking, especially with multiple commits, test your branch to ensure that the changes work as expected without introducing errors.
  • Handle conflicts promptly: Cherry-picking can often lead to conflicts, particularly if the changes apply to parts of the code that have diverged. Resolve conflicts as they arise.
  • Maintain clean history: If cherry-picking creates a messy history, consider using interactive rebase (git rebase -i) to tidy up before pushing to a shared repository.

For further reading on the git-cherry-pick command, see the official Git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2