Cherry-Picking Commits

Reviewed & published by Brayan K

Sometimes you need just one commit from another branch — an urgent fix, not the whole feature. git cherry-pick is the surgical tool for exactly that. By the end of this lesson you'll copy single commits, ranges of commits, and handle the conflicts that can arise.

Part of the free Git course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

What You'll Learn

1️⃣ Cherry-Picking a Single Commit

The core workflow: find the commit's hash on its source branch, switch to the branch that should receive it, then run git cherry-pick <hash>. Git applies that commit's changes as a new commit on your current branch — the original stays put on its own branch.

# Scenario: a bug fix landed on "feature", but you need JUST that one
# commit on main right now — without merging the whole unfinished feature.

# 1) Find the commit's hash on the source branch
git log --oneline feature
#   c3d4e5f Fix null-pointer crash   <- we want only this one
#   b2c3d4e Add half-finished form

# 2) Switch to the branch that should RECEIVE the fix
git switch main

# 3) Cherry-pick it — its changes are applied as a NEW commit on main
git cherry-pick c3d4e5f

2️⃣ Multiple Commits & the -x Flag

You're not limited to one. List several hashes to pick them in order, or use a range A..B (which excludes A and includes everything after it up to B). Add -x to stamp each new commit with a "cherry picked from commit ..." line, so you can always trace a fix back to its source.

# Cherry-pick can take more than one commit.

# Several specific commits (applied in the order listed)
git cherry-pick a1b2c3d e5f6g7h

# A RANGE of commits — note A..B EXCLUDES A, includes everything after it
git cherry-pick c3d4e5f..f6g7h8i

# Record where each came from with -x (adds a "cherry picked from" line),
# handy for tracing the fix back to its origin later.
git cherry-pick -x c3d4e5f

3️⃣ Handling Conflicts

Since cherry-pick replays a commit on a different base, its changes can clash with what's already there — a normal merge conflict. Resolve the markers, stage the file with git add, then run git cherry-pick --continue (not git commit). To abandon the whole operation, use git cherry-pick --abort.

# Because cherry-pick re-applies a commit on a different base, it can
# conflict — just like a merge or rebase.

git cherry-pick c3d4e5f
#   CONFLICT (content): Merge conflict in app.js

# 1) Open the file, resolve the <<<<<<< / ======= / >>>>>>> markers
# 2) Stage the resolved file
git add app.js

# 3) Finish the cherry-pick (do NOT run git commit)
git cherry-pick --continue

# Changed your mind? Undo the whole thing:
git cherry-pick --abort

4️⃣ Cherry-Picking a Merge Commit

This is the one that stops people. A merge commit has two parents, so "the changes it introduced" is ambiguous — Git refuses to guess:

Tell it which parent to treat as the mainline — the branch you were on when you merged — and the diff is taken against that side. Almost always -m 1:

-m 1 means "first parent" — the branch that received the merge — so you get everything the merged branch brought in. -m 2 takes the other side and is rarely what you want. Check with git log --oneline <hash>^1 and ^2 if you're unsure which is which.

5️⃣ Staging Without Committing (-n)

Sometimes you want a commit's changes but not its commit — to edit them first, or to squash several picks into one. -n (or --no-commit) applies the change and leaves it staged:

Pick several with -n in a row and they all pile up in the index, so one git commit squashes them together. Note that a -n pick leaves the operation open — git cherry-pick --quit clears that state without undoing your staged work.

6️⃣ --skip: The Third Option During a Conflict

When you pick a range and one commit turns out to be already applied — or its change is no longer wanted — you don't have to abort the whole run. git cherry-pick --skip drops just that commit and carries on with the rest of the range. So mid-conflict you have three exits, not two: --continue to keep it, --skip to drop it, --abort to undo everything.

Your turn. Copy one bug-fix commit from feature onto main. Fill in the two blanks.

# 🎯 YOUR TURN — replace each ___ then run it in your terminal.
# Goal: copy one bug-fix commit from feature onto main.

# 1) Find the fix commit's hash on the feature branch
git log --oneline feature

# 2) Switch to the branch that should RECEIVE it
git ___ main                   # 👉 the command that changes branch

# 3) Apply just that one commit onto main
git ___ <hash>                 # 👉 the command that copies a single commit

# ✅ Expected: "git log --oneline" on main now shows the fix as a NEW
#    commit (different hash), and the file change is present on main.

Common Errors (and the fix)

Pro Tips

📋 Quick Reference

CommandPurpose
git cherry-pick <hash>Apply one commit to the current branch
git cherry-pick A B CApply several commits in order
git cherry-pick A..BApply a range (excludes A)
git cherry-pick -x <hash>Record the source commit in the message
git cherry-pick -m 1 <merge>Pick a merge commit, relative to its first parent
git cherry-pick -n <hash>Apply and stage, but do not commit
git cherry-pick --continueFinish after resolving a conflict
git cherry-pick --skipDrop the current commit, continue the range
git cherry-pick --quitEnd the operation, keep what is staged
git cherry-pick --abortCancel and restore the branch

Mini-Challenge: Hotfix Across Branches

No commands given this time — just the plan. Move a single fix from a dev branch to main without the surrounding work.

# 🎯 MINI-CHALLENGE: hotfix one commit across branches.
# No commands given — just the plan. Run each step in your terminal.
#
# 1. On a "dev" branch, make 3 commits; ONE of them is an urgent bug fix
# 2. Note the bug-fix commit's hash with "git log --oneline"
# 3. Switch to main (which should NOT get the other two commits)
# 4. Cherry-pick ONLY the bug-fix commit onto main
# 5. Verify with "git log --oneline" that main got just that one change
#
# ✅ Expected: main contains the bug fix as a new commit, but none of the
#    other two dev commits.

# your commands here

🎉 Lesson Complete!

Practice quiz

What does `git cherry-pick <hash>` do?

  • Deletes a commit
  • Merges a whole branch
  • Applies the changes from one specific commit onto your current branch
  • Renames a commit

Answer: Applies the changes from one specific commit onto your current branch. Cherry-pick copies the changes of a single commit onto the branch you're on.

Before cherry-picking, which branch should you be on?

  • The branch you want to RECEIVE the commit
  • The branch the commit came from
  • Always main
  • It doesn't matter

Answer: The branch you want to RECEIVE the commit. Cherry-pick applies the commit to your current branch, so switch to the destination first.

Does the cherry-picked commit keep the original commit's hash?

  • Yes, identical
  • Only if it's annotated
  • Only on main
  • No — it becomes a new commit with a new hash

Answer: No — it becomes a new commit with a new hash. Cherry-pick creates a new commit (different parent), so it gets a new hash.

A good use of cherry-pick is...

  • Copying an entire branch
  • Pulling one urgent bug-fix commit into a release branch
  • Deleting old commits
  • Renaming branches

Answer: Pulling one urgent bug-fix commit into a release branch. Cherry-pick is ideal for grabbing a single needed commit without merging everything.

Can you cherry-pick more than one commit at once?

  • Yes — list several hashes or a range
  • No, only one ever
  • Only two
  • Only with merge

Answer: Yes — list several hashes or a range. You can pass multiple hashes, or a range like A..B, to cherry-pick several commits.

If a cherry-pick causes a conflict, after resolving you run...

  • git commit --pick
  • git merge --continue
  • git cherry-pick --continue
  • git rebase --continue

Answer: git cherry-pick --continue. Resolve, git add, then git cherry-pick --continue to finish.

How do you abandon an in-progress cherry-pick?

  • git reset --hard
  • git cherry-pick --abort
  • Ctrl+C
  • git cherry-pick --undo

Answer: git cherry-pick --abort. git cherry-pick --abort restores your branch to before the cherry-pick.

What does the `-x` flag add when cherry-picking?

  • Nothing
  • Extra conflict markers
  • A new branch
  • A line recording which original commit it came from

Answer: A line recording which original commit it came from. -x appends a '(cherry picked from commit ...)' note to the new commit's message.

How does cherry-pick differ from merge?

  • They are the same
  • Merge brings in a whole branch's history; cherry-pick takes individual commits
  • Cherry-pick brings more commits
  • Merge can't cause conflicts

Answer: Merge brings in a whole branch's history; cherry-pick takes individual commits. Merge integrates an entire branch; cherry-pick grabs only the specific commits you name.

After cherry-picking commit B onto main, where do the CHANGES from B live?

  • Only on the source branch
  • Deleted from both
  • On main as a new commit (and still on the source branch)
  • Only in the stash

Answer: On main as a new commit (and still on the source branch). The original stays put; a copy of its changes now also exists on main as a new commit.

Continue this course

Frequently asked questions

What is git cherry-pick?

git cherry-pick applies the changes introduced by one specific commit onto your current branch, creating a new commit there. It's like saying 'I want exactly this change here, but not the rest of the branch it came from.' You switch to the branch that should receive the change, then cherry-pick the commit by its hash.

When should I use cherry-pick instead of merge?

Use merge when you want a whole branch's history integrated. Use cherry-pick when you only need one or a few specific commits — for example, pulling a single urgent bug fix from a development branch into a release branch without dragging along unfinished features. It's surgical: you pick exactly the commits you want.

Does cherry-pick move or copy the commit?

It copies. The original commit stays exactly where it was on its source branch, and a new commit with the same changes (but a new hash, because its parent is different) is added to your current branch. So after cherry-picking, the change exists in two places. If you later merge the branches, Git is usually smart enough to recognise the duplicate.

What happens if a cherry-pick conflicts?

Because the commit is being applied on top of a different base, its changes can clash with what's already there, producing a normal merge conflict. Resolve the conflict markers in the affected files, stage them with git add, then run git cherry-pick --continue. If you'd rather not proceed, git cherry-pick --abort returns your branch to its state before the cherry-pick.

Can I cherry-pick several commits at once?

Yes. You can list multiple hashes (git cherry-pick A B C), and they're applied in the order given. You can also use a range with A..B, which applies every commit after A up to and including B (note that A itself is excluded). The -x flag adds a 'cherry picked from commit ...' note to each new commit so you can trace it back to the original.

Related lessons