Skip to content

Meet Graphite Agent — your collaborative AI reviewer, built right into your PR page.

Read more

Applying specific commits using git cherry-pick

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

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


Table of contents

git cherry-pick applies a specific commit from one branch onto another. Instead of merging an entire branch, you pick an individual commit and apply its changes on your current branch. It creates a new commit with a different SHA but the same content as the original.

This command is useful when you want just one commit's change without merging unrelated changes from its source branch.

Use git cherry-pick for targeted scenarios like:

  • Hotfixes: A bug is fixed on a feature branch, but the fix is needed immediately on main. Cherry-pick only that commit.
  • Backporting: You want to apply a fix to an older release branch without merging new features.
  • Commit recovery: You closed a PR or lost a branch but want to recover a specific commit.
  • Shared utility code: A helpful function lives on another branch and you want to reuse it without pulling in all changes.

If you find yourself cherry-picking more than a few commits or repeatedly, it's often a sign that rebasing or merging may be a better strategy.

Here's how to apply a single commit from one branch to another.

  1. find the commit hash Use git log --oneline <branch> or git log to locate the commit you want. Copy the hash (e.g., 3a1b2c3).

  2. checkout the target branch

    Terminal
    git checkout main
  3. apply the commit

    Terminal
    git cherry-pick <commit-hash>

    Git will apply the change as a new commit on your current branch. If successful, it will auto-commit and advance the branch.

  4. push the result

    Terminal
    git push origin main

If you're cherry-picking a range:

Terminal
git cherry-pick A^..C

This applies commits A through C (inclusive), assuming they are linear.

Conflicts happen when changes in the cherry-picked commit overlap with changes in your current branch.

When Git reports a conflict:

  1. check status

    Terminal
    git status
  2. resolve conflicts manually Open files with conflict markers (<<<<<<<, =======, >>>>>>>) and edit them to keep the correct version.

  3. stage resolved files

    Terminal
    git add <file>
  4. continue cherry-pick

    Terminal
    git cherry-pick --continue

If you want to cancel:

Terminal
git cherry-pick --abort

To skip a problematic commit in a range:

Terminal
git cherry-pick --skip

Say your history looks like this:

Terminal
main: A---B---C
\
feature: D---E

Commit E on feature contains a bug fix. To apply just E to main:

Terminal
git checkout main
git cherry-pick <hash-of-E>

You now have:

Terminal
main: A---B---C---E'

The commit E' is a new commit with the same changes as E, but a different SHA.


  • Duplicate commits: If you cherry-pick a commit and later merge the source branch, you'll get duplicate changes. Use git cherry-pick -x to record the origin in the commit message for traceability.

  • Missing dependencies: Some commits depend on earlier changes. Cherry-picking them alone might break builds or tests. Always review the context of the commit.

  • Too many cherry-picks: If you're cherry-picking entire sections of work, consider merging or rebasing instead. Cherry-picking is ideal for one-off commits.

  • Remote commits: To cherry-pick a commit from a remote branch, first fetch it:

    Terminal
    git fetch origin feature-x
    git cherry-pick <commit-hash>
  • Avoiding confusion: Communicate with your team if you cherry-pick. It's easy to confuse history if a commit appears in multiple branches with different hashes.

Graphite is a Git-centric tool that enhances workflows around stacked pull requests. Its CLI, commonly invoked with gt, sits on top of vanilla Git and offers features like visual logs, branch stacking, automatic restacking, and simplified rebasing. It’s fully compatible with Git – your regular Git commands still work alongside it. To learn more about how you can use the Graphite CLI alongside Git, check out this documentation.

git cherry-pick is a precision tool for applying individual commits across branches. It's perfect for urgent fixes, shared utilities, or backporting without merging unrelated work. Use it when the commit you want is self-contained and doesn't introduce dependencies.

That said, overuse can complicate history — especially if those cherry-picked commits eventually get merged too. Use sparingly, understand the context of what you're picking, and prefer merge or rebase for broader changes.

Built for the world's fastest engineering teams, now available for everyone