shellstreak

git cherry-pick: copy one commit to another branch

Updated · Every command on this page ran in our practice shell

Short answer

Switch to the branch that needs the commit, find the commit's hash with git log --oneline other-branch, and run git cherry-pick <hash>. Git copies that one commit as a new commit. On a conflict, fix the file, git add it and run git cherry-pick --continue, or git cherry-pick --abort to cancel.

Sometimes one commit on a branch is needed somewhere else now: a bug fix on a feature branch that isn't ready to merge, say. git cherry-pick copies that single commit onto the branch you're on, without bringing anything else along. The examples below ran in a practice shell on a recipes repository with a seasonal branch.

Find the commit you want

List the other branch's commits and note the hash of the one you need. Here, seasonal has three commits main doesn't have, and only Fix oven temperature is needed on main.

example
learner@shellstreak:~/recipes$ git log --oneline seasonal
0dfff39 (seasonal) Add spice list
cd616bb Fix oven temperature
741fce0 Add pumpkin bread
7a76ad2 (HEAD -> main) Add muffin recipe
ba602fb Add scone recipe
4df7d66 Add bread recipe

Copy it with git cherry-pick

Run git cherry-pick <hash> on the branch that should receive the commit. Instead of a hash you can use any revision name: seasonal~1 means one commit before the tip of seasonal. The copy is a new commit with a new hash but the same message, author and changes.

example
learner@shellstreak:~/recipes$ git cherry-pick seasonal~1 && git log --oneline -n 2
[main 7c3106b] Fix oven temperature
 Date: Sun Mar 1 09:39:00 2026 +0000
 1 file changed, 1 insertion(+), 1 deletion(-)
7c3106b (HEAD -> main) Fix oven temperature
7a76ad2 Add muffin recipe

Pick several commits

List several commits to copy them in order: git cherry-pick A B. Each becomes its own new commit. Real git also accepts a range, A..B, which means everything after A up to B.

example
learner@shellstreak:~/recipes$ git cherry-pick seasonal~2 seasonal && git log --oneline -n 3
[main 8f94b77] Add pumpkin bread
 Date: Sun Mar 1 09:37:00 2026 +0000
 1 file changed, 3 insertions(+)
 create mode 100644 pumpkin.txt
[main 70152f5] Add spice list
 Date: Sun Mar 1 09:41:00 2026 +0000
 1 file changed, 3 insertions(+)
 create mode 100644 spices.txt
70152f5 (HEAD -> main) Add spice list
8f94b77 Add pumpkin bread
7a76ad2 Add muffin recipe

When a cherry-pick conflicts

If your branch changed the same lines, the cherry-pick stops with conflict markers, just like a merge. git status says You are currently cherry-picking. Fix the file, git add it, then run git cherry-pick --continue.

example
learner@shellstreak:~/recipes$ git status
On branch main
You are currently cherry-picking commit cd616bb.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   bread.txt

no changes added to commit (use "git add" and/or "git commit -a")

Resolve and continue

Here main said Bake at 230C with steam and the fix said Bake at 220C. Keep both ideas, add the file, and continue. Git reuses the original commit message; in a real terminal --continue opens it in your editor first, so save and close (in vim: Esc, then :wq).

example
learner@shellstreak:~/recipes$ printf 'Country loaf\nflour 500g\nwater 350g\nsalt 10g\nBake at 220C with steam\n' > bread.txt && git add bread.txt && git cherry-pick --continue
[main 1b1da83] Fix oven temperature
 Date: Sun Mar 1 09:39:00 2026 +0000
 1 file changed, 1 insertion(+), 1 deletion(-)

Give up with --abort

git cherry-pick --abort cancels the whole thing and puts your branch back where it was. (--skip drops just the current commit when you're picking several.)

example
learner@shellstreak:~/recipes$ git cherry-pick --abort && git status
On branch main
nothing to commit, working tree clean

Rescue a lost commit

Cherry-pick works on any commit you can name, including one that's no longer on any branch. If a force-push or reset removed a commit, find it in git reflog and cherry-pick it back. Here the agent's force-push wiped Add gift card terms; it's still at HEAD@{2}. To move a whole branch back instead, git reset --keep HEAD@{1} puts it where it was one move ago, and refuses if that would wipe uncommitted edits.

example
learner@shellstreak:~/kiosk$ git reflog
97c952b (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: commit: Raise timeout
18a3013 HEAD@{1}: reset: moving to HEAD~1
82f2791 HEAD@{2}: pull -q --ff-only: Fast-forward
18a3013 HEAD@{3}: commit: Add cart tests
28d2f8b HEAD@{4}: commit (initial): Create kiosk app

Check yourself

One real exercise from the “Git history & undo” unit. No account needed.

Copy only the Fix oven temperature commit from seasonal onto main (it's the one just before seasonal's tip). Leave the pumpkin and spice commits behind.

What's checked: the files recipes/bread.txt, recipes/pumpkin.txt, recipes/spices.txt; the repository's current branch, commits.

Common mistakes

  • Cherry-picking a whole feature commit by commit. If you need all of it, merge the branch instead.

  • Cherry-picking a fix and later merging the branch anyway. It usually works, but the same change now exists as two commits; mention the original hash in the message (real git's -x flag does this for you).

  • Picking onto the wrong branch. The commit lands wherever HEAD is; check git branch first.

  • Forgetting git add before git cherry-pick --continue.

Questions

What's the difference between cherry-pick and merge?+

Merge brings in every commit from the other branch and records that the two were joined. Cherry-pick copies only the commits you name, as new commits, and the branches stay unrelated.

Does cherry-pick remove the commit from the original branch?+

No. The original stays where it was; you get a copy. If you want it gone from the other branch too, remove it there with `git revert` or, if unpushed, `git reset`.

Can I cherry-pick from another repository?+

Only after its commits are in yours: add it as a remote and `git fetch` it, then cherry-pick by hash.

Reading is step one. Now make it stick.

Practise this in a five-minute lesson with instant feedback. No account needed.

Practise it now