Git Cherry-Pick

Copy specific commits from one branch to another with cherry-pick, pick ranges, handle conflicts and know when not to use it.

Lesson 14 of 30Advanced17 min read

Sometimes you don't want a whole branch, just one commit from it. A bug fix was committed on a feature branch but is needed on main today. A fix made on main must also go into the older release-1.x branch that customers are still using. Merging would bring along unrelated work you're not ready to ship.

git cherry-pick copies the changes from one or more chosen commits and applies them as new commits on your current branch. The original commits stay where they are; your branch gets a copy with the same changes and message, but a new hash.

In this lesson you'll cherry-pick single commits and ranges, record where a copy came from, handle conflicts, and learn when cherry-picking is the right tool and when it quietly makes history harder to follow.

Picking a commit

Terminal
git log --oneline feature      # find the commit's hash
git switch main
git cherry-pick 4b9a8c3
  • Switch to the branch that should receive the change, then cherry-pick the commit's hash.
  • Git applies that commit's changes and makes a new commit with the same message and author.
  • The new commit has a different hash, because it has a different parent.
  • You can pick from any branch, or even from a commit that's no longer on any branch, as long as you know its hash.

Several commits and useful options

CommandWhat it does
git cherry-pick a1b2c3 d4e5f6Pick several commits, in order
git cherry-pick a1b2c3..d4e5f6Pick the commits after a1b2c3 up to d4e5f6
git cherry-pick a1b2c3^..d4e5f6The same range, but including a1b2c3
git cherry-pick -x a1b2c3Add "(cherry picked from commit ...)" to the message
git cherry-pick -n a1b2c3Apply the changes but don't commit yet

Use -x whenever you copy fixes between long-lived branches, such as from main to a release branch. The note in the message lets anyone trace the copy back to the original.

Conflicts

If the target branch has changed the same lines, the cherry-pick stops with a conflict, exactly like a merge:

  1. Fix the conflicted files and remove the markers.
  2. git add them.
  3. git cherry-pick --continue.

git cherry-pick --abort cancels and returns your branch to how it was. When you pick several commits, Git stops at each one that conflicts, so you resolve them one at a time.

When to cherry-pick, and when not to

Cherry-pick is ideal for:

  • Hotfixes: copying an urgent fix to main or a release branch.
  • Backports: bringing a fix into an older supported version.
  • Rescuing work: grabbing one good commit from an abandoned branch.

It's the wrong tool for routinely moving work between branches. Every cherry-pick creates a duplicate commit; if you later merge the original branch too, Git usually handles the duplicate, but history becomes harder to read and conflicts become more likely. For bringing a whole branch up to date, use merge or rebase.

Git Cherry-Pick Syntax

Cherry-pick commands

git switch target-branch
git cherry-pick <hash>
git cherry-pick <hash1> <hash2>
git cherry-pick <from>^..<to>
git cherry-pick -x <hash>
git cherry-pick --continue
git cherry-pick --abort

Git Cherry-Pick Summary

  • git cherry-pick <hash> copies a commit's changes onto the current branch as a new commit.
  • Pick several commits by listing them, or a range with A..B (A^..B to include A).
  • Use -x to record the original commit in the message; -n applies without committing.
  • Resolve conflicts, git add, then git cherry-pick --continue; --abort cancels.
  • Use it for hotfixes and backports, not as a replacement for merge or rebase.

Git Cherry-Pick Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Copying a bug fix to main

Terminal
git log --oneline search-box -3
git switch main
git cherry-pick 6c2d8e1
git log --oneline -2
Output
4b9a8c3 Style search box
6c2d8e1 Fix crash when cart is empty
2d6f1e4 Add search box
Switched to branch 'main'
[main 0a9b8c7] Fix crash when cart is empty
 Date: Tue Sep 3 10:12:44 2024 +0100
 1 file changed, 3 insertions(+), 1 deletion(-)
0a9b8c7 (HEAD -> main) Fix crash when cart is empty
7e1c0d2 Fix footer link
Line-by-line explanation
  1. Line 1The fix (6c2d8e1) was made on the search-box branch, between two unfinished commits.
  2. Line 3Copy only the fix onto main. The search box work stays behind.
  3. Line 4The copy has a new hash, 0a9b8c7, but the same message.
Example 2

Backporting with -x

Terminal
git switch release-1.x
git cherry-pick -x 0a9b8c7
git log -1 --format=%B
Output
[release-1.x 5e6f7a8] Fix crash when cart is empty
 1 file changed, 3 insertions(+), 1 deletion(-)
Fix crash when cart is empty

(cherry picked from commit 0a9b8c7)
Line-by-line explanation
  1. Line 1The older release branch that customers still use.
  2. Line 2-x records where the fix came from.
  3. Line 3%B prints the full message, including the extra line.
Example 3

Picking a range of commits

Terminal
git log --oneline experiments -4
git switch main
git cherry-pick 3a1b2c4^..9d8e7f6
Output
9d8e7f6 Add keyboard shortcuts
5c4b3a2 Add shortcut help panel
3a1b2c4 Refactor key handling
1f0e9d8 Try a new colour theme
[main 2b3c4d5] Refactor key handling
[main 6e7f8a9] Add shortcut help panel
[main a0b1c2d] Add keyboard shortcuts
Line-by-line explanation
  1. Line 1Four commits on the experiments branch; we want the three about keyboard shortcuts, not the colour theme.
  2. Line 3^ makes the range include 3a1b2c4 itself. The three commits are applied in order.

Common Git Cherry-Pick Mistakes

Cherry-picking from the wrong branch

Don't
git switch feature
git cherry-pick 6c2d8e1   # meant to copy INTO main
Do
git switch main
git cherry-pick 6c2d8e1

Always switch to the branch that should receive the commit first.

Forgetting ^ in a range

Don't
git cherry-pick 3a1b2c4..9d8e7f6    # skips 3a1b2c4
Do
git cherry-pick 3a1b2c4^..9d8e7f6   # includes it

Cherry-picking instead of merging a whole branch

Don't
git cherry-pick a1 b2 c3 d4 e5 f6 g7   # every commit, one by one
Do
git merge feature

Git Cherry-Pick in Real Projects

Cherry-pick in release management

Projects that support several versions at once, such as programming languages and frameworks, fix bugs on the main branch first and then cherry-pick the fixes back to each supported release branch, marking them with -x. Teams also cherry-pick an urgent fix straight to production while the rest of a feature is still under review.

Git Cherry-Pick FAQ

How do I cherry-pick a merge commit?

Git refuses with "is a merge but no -m option was given", because a merge commit has two parents and it doesn't know which side's changes you want. Add -m 1 to apply the changes relative to the first parent, usually the branch that was merged into: git cherry-pick -m 1 <hash>.

Can I cherry-pick from a fork or another repository?

Yes, once Git has the commit. Add the other repository as a remote with git remote add other <url>, run git fetch other, then cherry-pick the commit by its hash. The commit must be fetched into your repository before Git can apply it.

Does cherry-pick keep the same commit hash?

No. A cherry-pick creates a new commit with the same changes and message but a different parent, so it gets a different hash. Add -x to append "(cherry picked from commit ...)" to the message so you can trace it back to the original.

Will merging later duplicate a commit I cherry-picked?

Usually the merge goes through cleanly, because Git sees that both branches made the same change. The history will contain both commits, the original and the copy. git cherry -v main feature marks commits on feature whose changes are already in main with a -.

How do I continue after fixing a cherry-pick conflict?

Edit the conflicted files, run git add on them, then git cherry-pick --continue. If you're picking several commits, Git moves on to the next one. git cherry-pick --abort gives up and returns the branch to where it was before you started.

Git Cherry-Pick Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the command that copies commit a1b2c3d onto the current branch.

git  a1b2c3d
Multiple choiceeasy

Does cherry-pick move the original commit?

Multiple choicemedium

What does the -x option add?

Predict the outputmedium

Which commits does git cherry-pick A..C apply, if the history is A then B then C?

Multiple choicehard

When is cherry-pick a poor choice?

Git Cherry-Pick Quiz

5 questions to check what you learned.

  1. Which branch should you be on before cherry-picking?

    • The branch that should receive the commit
    • The branch the commit came from
    • main, always
    • Any branch
  2. How do you continue after resolving a cherry-pick conflict?

    • git commit -m "fix"
    • git merge --continue
    • git cherry-pick --skip
    • git add the files, then git cherry-pick --continue
  3. What does git cherry-pick -n do?

    • Picks the newest commit
    • Applies the changes without committing them
    • Skips conflicts
    • Creates a new branch
  4. What is a backport?

    • Deleting old branches
    • Pushing to a backup server
    • Reverting a commit
    • Copying a fix from a newer branch into an older supported version
  5. Why do small, focused commits make cherry-picking easier?

    • A focused commit contains only the change you want, so it applies cleanly elsewhere
    • Git can only pick small commits
    • Small commits have shorter hashes
    • They don't need messages