Post

Git Cherry Pick

Apply specific commits from one branch to another without merging the whole branch.

Cherry-pick lets you apply a specific commit from one branch onto another. You get just that commit — not the entire branch history.

When to use it

  • A bug fix is on a feature branch but main needs it now
  • You merged a branch but only want one of its commits on another branch
  • A commit landed on the wrong branch — move it without rebasing everything
  • Backporting a fix to an older release branch

What’s involved

Every commit has a hash. Cherry-pick takes that hash and replays the commit’s diff on your current branch as a new commit.

1
2
3
4
git log --oneline feature/my-feature
# a3f2c1d Fix null pointer in cart
# b9e4a2c Add cart feature
# ...

You want just the fix on main:

1
2
git checkout main
git cherry-pick a3f2c1d

Git creates a new commit on main with the same changes. The hash will be different — it’s a new commit.

Cherry-pick a range of commits

1
git cherry-pick abc123..def456

This picks commits from abc123 (exclusive) up to def456 (inclusive).

To include abc123:

1
git cherry-pick abc123^..def456

Cherry-pick without committing

Apply the changes to your working directory but don’t commit yet:

1
git cherry-pick --no-commit a3f2c1d

Useful when you want to combine changes from multiple commits into one.

Conflicts

If the cherry-picked commit conflicts with your current branch:

  1. Git pauses and shows the conflict
  2. Resolve the conflict in the file
  3. Stage the resolved file: git add <file>
  4. Continue: git cherry-pick --continue

Or bail out completely:

1
git cherry-pick --abort

Deploy to remote

The typical workflow is to cherry-pick onto a new branch and push that for review:

1
2
3
git checkout -b fix/cart-null-pointer
git cherry-pick a3f2c1d
git push -u origin fix/cart-null-pointer

Then open a PR from fix/cart-null-pointer into your target branch (main, develop, etc).

If you need to push to an existing remote branch you’ve already pushed before and the history has changed (e.g. after a rebase):

1
git push --force-with-lease origin fix/cart-null-pointer

--force-with-lease is safer than --force — it aborts if someone else has pushed to the branch since you last fetched.

Cherry-pick vs merge vs rebase

 What it does
mergeBrings in all commits from a branch
rebaseReplays all commits of a branch onto another
cherry-pickPicks specific commits only

Use cherry-pick when you want surgical precision — one or a few commits, not the whole branch.

See also

This post is licensed under CC BY 4.0 by the author.