|$ curl https://forge-ai.dev/api/markdown?path=docs/git/cherry-pick
$cat docs/git-cherry-pick.md
updated Recently·18 min read·published

Git Cherry-pick

GitIntermediate🎯Free Tools
Introduction

git cherry-pick applies a specific commit from one branch to another without merging the entire branch. It creates a new commit with the same changes, useful for backporting fixes, recovering lost work, or selectively applying changes.

Basic Cherry-pick
cherry-pick-basic.sh
Bash
1# Cherry-pick a single commit
2git cherry-pick abc1234
3
4# Cherry-pick without committing (stage changes only)
5git cherry-pick --no-commit abc1234
6# Review changes, then commit manually
7git commit -m "Backport: fix login validation"
8
9# Cherry-pick with a custom commit message
10git cherry-pick -m 1 -M "Backport fix to v1.x branch" abc1234
11
12# Cherry-pick from a remote branch
13git fetch origin
14git cherry-pick origin/feature-branch~3 # 3 commits back from tip
15
16# View the commit before cherry-picking
17git show abc1234

info

Use --no-commit to stage changes without creating a commit. This lets you review the diff, make adjustments, and commit with a custom message.
Cherry-pick Ranges
cherry-pick-ranges.sh
Bash
1# Cherry-pick a range of commits (exclusive of start)
2git cherry-pick abc1234..def5678
3# Applies commits after abc1234 up to and including def5678
4
5# Cherry-pick inclusive range (use ^ to include the start)
6git cherry-pick abc1234^..def5678
7# Includes abc1234 through def5678
8
9# Cherry-pick multiple non-contiguous commits
10git cherry-pick abc1234 def5678 ghi9012
11
12# Cherry-pick last N commits from a branch
13git cherry-pick main~3..main
14
15# Cherry-pick with merge commit preservation
16git cherry-pick -m 1 abc1234
17# -m 1 keeps the parent side of the merge (usually main)
18# -m 2 keeps the merged branch side
Conflict Resolution

Cherry-pick conflicts are similar to merge conflicts. Git pauses and asks you to resolve them before continuing.

cherry-pick-conflicts.sh
Bash
1# Cherry-pick with conflicts
2git cherry-pick abc1234
3# CONFLICT (content): Merge conflict in src/auth.ts
4
5# Resolve conflicts
6# 1. Edit the conflicting files
7# 2. Stage the resolved files
8git add src/auth.ts
9
10# Continue cherry-picking
11git cherry-pick --continue
12
13# Or abort and start over
14git cherry-pick --abort
15
16# Skip a problematic commit (in a range)
17git cherry-pick --skip
18
19# Useful during large range cherry-picks
20git cherry-pick abc1234..xyz9999
21# If one commit conflicts:
22git cherry-pick --skip # Skip the bad one, continue
23
24# View cherry-pick state
25git status

warning

Cherry-pick conflicts can be tricky because the commit was originally written against different surrounding code. Take time to understand the original intent, not just the diff.
Common Use Cases
use-cases.sh
Bash
1# 1. Backport a bugfix to a release branch
2git checkout release/1.x
3git cherry-pick abc1234 # The fix from main
4
5# 2. Recover a commit that was accidentally reverted
6git cherry-pick abc1234 # The reverted commit
7
8# 3. Apply a fix from open source to your fork
9git remote add upstream https://github.com/original/repo.git
10git fetch upstream
11git cherry-pick upstream/abc1234
12
13# 4. Move a commit between branches (rebase alternative)
14git checkout feature-v2
15git cherry-pick main~1 # Grab the latest commit from main
16
17# 5. Cherry-pick with sign-off (DCO compliance)
18git cherry-pick -s abc1234
19# Adds Signed-off-by: Your Name <email> to commit message

best practice

Use cherry-pick sparingly. Frequent cherry-picking suggests your branching strategy needs adjustment. If you regularly backport to multiple release branches, consider a release-flow workflow or automated backport scripts.
Advanced Patterns

Extra depth for production teams — conflict strategies, automation, and recovery.

Automation-friendly flags

automation.sh
Bash
1git status --porcelain=v1
2git diff --name-only --diff-filter=ACMR
3git log -1 --pretty=format:%H
4git merge-base HEAD origin/main
5git rev-list --count origin/main..HEAD

Recovery drill

recovery.sh
Bash
1git reflog | head -20
2git fsck --lost-found | head
3git branch rescue HEAD@{1}
4git log --oneline rescue -5
🔥

pro tip

Practice recovery in /tmp labs before you need it on a deadline.
Production Checklist
  • No secrets in history for this change set
  • CI green on the PR
  • Rebased or merged with latest main
  • Rollback plan: revert SHA known
  • Tags/releases updated if needed
prod-check.sh
Bash
1git status -sb
2git log --oneline origin/main..HEAD
3git diff --check
4git rev-parse HEAD
Additional Examples
more-a.sh
Bash
1# Cherry-pick a range onto a release branch
2git switch release/1.2
3git cherry-pick abc123^..def456
4# conflict?
5git status
6# fix, then:
7git add -A && git cherry-pick --continue
8# or abort:
9# git cherry-pick --abort
more-b.sh
Bash
1# Bisect with a script
2git bisect start
3git bisect bad HEAD
4git bisect good v1.0.0
5git bisect run ./scripts/test-bug.sh
6git bisect reset
more-c.sh
Bash
1# Submodule bump
2git submodule update --remote --merge libs/shared
3git add libs/shared
4git commit -m "chore(deps): bump shared submodule"
5git submodule status
more-d.sh
Bash
1# Workflow: trunk-based short PR
2git fetch origin
3git switch -c fix/timeout origin/main
4# change + test
5git commit -am "fix: request timeout"
6git push -u origin HEAD
7gh pr create --fill
8gh pr checks
9gh pr merge --squash --delete-branch
Cherry-pick Conflict & Sequencing Deep Dive

Cherry-pick applies the diff of a commit onto HEAD. When multiple commits are picked, order matters — apply oldest first unless you intentionally reverse.

Sequencing

cp-seq.sh
Bash
1# Oldest first
2git cherry-pick abc111 def222 ghi333
3# Empty commit when change already present:
4git cherry-pick --skip
5# Keep original author when picking:
6git cherry-pick --keep-redundant-commits abc111

Conflict resolution

cp-conflict.sh
Bash
1git cherry-pick abc123
2# CONFLICT
3git status
4git diff
5# edit files, remove markers
6git add -A
7git cherry-pick --continue
8# give up:
9git cherry-pick --abort

Backport recipe

cp-backport.sh
Bash
1git fetch origin
2git switch -c backport/fix-timeout origin/release/1.2
3git cherry-pick <sha-from-main>
4git push -u origin HEAD
5gh pr create --base release/1.2 --fill

best practice

If you cherry-pick often, your branching strategy may need release branches or automated backports.

Vs rebase vs merge

  • cherry-pick: copy select commits onto another line
  • rebase: replay your whole branch onto a new base
  • merge: join histories with a merge commit
cp-lab.sh
Bash
1rm -rf /tmp/cp-lab && mkdir /tmp/cp-lab && cd /tmp/cp-lab
2git init -b main
3echo a > f && git add f && git commit -m a
4git switch -c topic
5echo b > f && git commit -am b
6echo c > f && git commit -am c
7git switch main
8git cherry-pick topic~1 # pick middle commit 'b'
9cat f && git log --oneline
Agent Notes — Cherry-pick
  • Prefer explicit SHAs from git log --oneline
  • Always state target branch before picking
  • On conflict, never leave markers; prefer --abort if unsure
  • Do not cherry-pick merge commits without -m

info

See also Merge and Rebasing.
$Blueprint — Engineering Documentation·Section ID: GIT-CP-01·Revision: 1.0

Community

Get help on Slack, Discord or VIP

Stuck on a guide? Join the community and ask.