More Git answers

by Danny
Published 2 min read

Copy one commit, or a range of commits, from another branch onto your current branch with git cherry-pick, and handle conflicts, merges and duplicate commits.

The short answer

#

Switch to the branch that should receive the change, find the commit's hash on the other branch, and run git cherry-pick with that hash. Git applies the same change as a new commit on your current branch.


_10
git switch main
_10
git log --oneline feature/payments # find the commit you want
_10
git cherry-pick 4f2c9a1

Why it happens

#

Sometimes you need one specific change without merging a whole branch. Common cases are a bug fix made on a feature branch that also needs to go to main, or a fix on main that needs to go back to a release branch.

git cherry-pick works out the diff that a commit introduced compared with its parent, then applies that diff on top of your current branch and commits it. The new commit has the same message and author but a different hash, because its parent is different. Git treats it as a separate commit from the original.

How to fix it

#

Pick a single commit


_10
git switch release/2.3
_10
git cherry-pick 4f2c9a1

It is good practice to add -x. It appends a line such as "(cherry picked from commit 4f2c9a1...)" to the message, so anyone reading the history can trace where it came from:


_10
git cherry-pick -x 4f2c9a1

Pick several commits or a range

List hashes to apply them in order:


_10
git cherry-pick 4f2c9a1 b71e0d3 9a0c442

For a run of consecutive commits, use a range. A..B means "everything after A, up to and including B", so add ^ to include A itself:


_10
git cherry-pick 4f2c9a1^..9a0c442

Apply the change without committing

-n (or --no-commit) applies the changes to your working tree and index but stops before committing. That is useful for combining several picks into one commit or adjusting the change first:


_10
git cherry-pick -n 4f2c9a1 b71e0d3
_10
git commit -m "Backport payment rounding fixes"

Handle conflicts

If the change does not apply cleanly, Git stops and marks the conflicting files, just as in a merge.


_10
git status # see the conflicted files
_10
# edit the files and remove the conflict markers
_10
git add src/payments.ts
_10
git cherry-pick --continue

You can also give up with git cherry-pick --abort, which returns the branch to how it was before you started, or skip just the current commit in a sequence with git cherry-pick --skip.

Cherry-pick a merge commit

A merge commit has two parents, so Git does not know which side's changes you want. Tell it with -m and the parent number, where 1 is usually the branch that was merged into:


_10
git cherry-pick -m 1 c3d7e88

Common pitfalls

#
  • Cherry-picking a commit that depends on earlier commits you did not pick. It may conflict or apply but leave code that does not build.
  • Cherry-picking and then later merging the same branch. Git usually notices identical changes, but if the code has moved on you can get confusing conflicts or a change applied twice.
  • Using cherry-pick to move lots of work between branches. If you need most of a branch, a merge or rebase keeps history clearer.
  • Expecting the same commit hash. The new commit always has a new hash.
  • Leaving out -x on shared branches, which makes it harder to see later that a fix was backported.
#

Was this resource helpful?