git cherry-pick: Copy Commits from Another Branch (with Examples)

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.

Advertisement

๐Ÿ“– 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 main to 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

OptionEffect
-xAdds “(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 1Cherry-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
CommandEffect
git cherry-pick --continueFinish after resolving
git cherry-pick --skipSkip this commit (when picking several)
git cherry-pick --abortCancel 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

  1. Create feature/x with three commits. On main, cherry-pick only the second one and check git log --oneline.
  2. Cherry-pick a range of two commits including the first one (A^..B syntax).
  3. Cherry-pick with -x and read the generated message.
  4. Use -n to apply two commits without committing, then make one combined commit.
  5. Cause a conflict during a cherry-pick, resolve it and continue; then repeat and abort.
  6. Commit to main by 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

๐ŸŽฌ 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)”

Leave a Comment