Sometimes you need just one commit from another branch, such as a bug fix for a release. git cherry-pick does exactly that. This lesson shows when to use it, how to pick single commits and ranges, useful options, conflicts and common real-world uses.
๐ What You’ll Learn: what cherry-pick does, when to use it, picking one commit, several commits and ranges, -x, -n, -e and -m options, resolving and aborting cherry-pick conflicts, moving a commit to the right branch, and cherry-pick workflows on GitHub.
๐ What Is git cherry-pick?
git cherry-pick copies one specific commit (or a few) from another branch and applies it to your current branch as a new commit. Unlike merge, it does not bring the whole branch, just the commits you pick.
main: C1 โโ C2 โโ C3
\
feature: F1 โโ F2 (bug fix) โโ F3
git switch main && git cherry-pick F2
main: C1 โโ C2 โโ C3 โโ F2' (same change as F2, new hash)
๐งช When to Use It
- Backport a bug fix from
mainto a release branch (release/2.3) without the new features. - Take one useful commit from a teammate’s experimental branch.
- Recover a commit that ended up on the wrong branch.
๐ Note: Prefer merge or rebase when you want a whole branch. Cherry-picking many commits duplicates history and can cause the same change to appear twice later.
โถ๏ธ Cherry-Pick One Commit
git log --oneline feature/login-fixes # find the commit hash
# 4d5e6f7 Fix flaky wait in login test
git switch release/2.3
git cherry-pick 4d5e6f7
[release/2.3 9a8b7c6] Fix flaky wait in login test
Date: Thu Oct 30 11:20:05 2026 +0530
1 file changed, 2 insertions(+), 3 deletions(-)
๐ข Several Commits and Ranges
git cherry-pick a1b2c3d e4f5a6b # specific commits, applied in order
git cherry-pick a1b2c3d..e4f5a6b # range: AFTER a1b2c3d up to e4f5a6b
git cherry-pick a1b2c3d^..e4f5a6b # range INCLUDING a1b2c3d
โ๏ธ Useful Options
| Option | Effect |
|---|---|
-x | Adds “(cherry picked from commit …)” to the message: great for traceability on release branches |
-n (--no-commit) | Applies the changes without committing, so you can combine or edit them first |
-e (--edit) | Edit the commit message before committing |
-m 1 | Cherry-pick a merge commit, using parent 1 as the base |
git cherry-pick -x 4d5e6f7
๐งฏ Conflicts During Cherry-Pick
If the change does not apply cleanly, Git stops with a conflict:
git cherry-pick 4d5e6f7
# CONFLICT (content): Merge conflict in tests/login.spec.ts
# edit the file to resolve, then:
git add tests/login.spec.ts
git cherry-pick --continue
| Command | Effect |
|---|---|
git cherry-pick --continue | Finish after resolving |
git cherry-pick --skip | Skip this commit (when picking several) |
git cherry-pick --abort | Cancel and return to the state before |
See Resolve Merge Conflicts for resolving the files themselves.
๐ Move a Commit to the Right Branch
Committed to main by mistake (and not pushed)?
git log --oneline -1 # note the hash, e.g. 7f8e9d0
git switch -c feature/new-tests # create the correct branch (it starts with the commit)
git switch main
git reset --hard HEAD~1 # remove the commit from main
Or, if the target branch already exists: switch to it, git cherry-pick 7f8e9d0, then remove it from main with git reset --hard HEAD~1 (only if not pushed; see Undo Changes).
๐ Cherry-Pick and GitHub
GitHub has no cherry-pick button for pull requests, but the workflow is simple: cherry-pick locally onto a new branch based on the release branch, push it, and open a pull request into the release branch. Many teams automate backports with a bot or a GitHub Action.
๐ Assignment
- Create
feature/xwith three commits. Onmain, cherry-pick only the second one and checkgit log --oneline. - Cherry-pick a range of two commits including the first one (
A^..Bsyntax). - Cherry-pick with
-xand read the generated message. - Use
-nto apply two commits without committing, then make one combined commit. - Cause a conflict during a cherry-pick, resolve it and continue; then repeat and abort.
- Commit to
mainby mistake and move the commit to a new branch.
โ FAQ
๐ค What does git cherry-pick do?
It applies the changes of one or more existing commits onto your current branch as new commits.
๐ค How do I cherry-pick a commit from another branch?
Find the hash with git log other-branch, switch to your target branch and run git cherry-pick <hash>.
๐ค How do I cherry-pick multiple commits?
List them (git cherry-pick a b c) or use a range: git cherry-pick A^..B includes A, A..B starts after A.
๐ค How do I abort a cherry-pick?
git cherry-pick --abort returns to the state before the cherry-pick started.
๐ค What is the difference between cherry-pick and merge?
Merge brings all commits of a branch and records the integration. Cherry-pick copies only the commits you choose, as new commits with new hashes.
๐ฏ Next Steps
- ๐ท๏ธ Git Tags and Versioning
- ๐ Pull Requests Step by Step
- ๐ Git and GitHub Tutorial: full course
๐ฌ Learn Git and GitHub step by step: watch the series on YouTube!
Subscribe to SqaToolsByDY • Practice on sqatools.in
1 thought on “git cherry-pick: Copy Commits from Another Branch (with Examples)”