Cherry-Picking Playbook
Safe cherry-pick workflows for bringing changes from another branch or remote into a local sync branch without merging whole history.
Cherry-Picking Playbook
This guide documents safe cherry-pick workflows for this repository, including:
- Cherry-picking all commits from one source branch.
- Cherry-picking selected commits one by one.
- Cherry-picking multiple source branches into one sync branch.
- Handling the case where the source feature branch is already merged upstream.
When To Use This
Use this playbook when you want to bring changes from another branch or remote (for example project-1) into a local sync branch (for example sync/from-project-1_13-07-26) without merging whole history.
1. Preconditions
- Ensure source remote exists (important for fresh clone):
git remote -vIf project-1 is not listed, add it:
git remote add project-1 <path-or-url-to-project-1>- Make sure your working tree is clean:
git status --short- Checkout your target branch (the branch that will receive commits):
git checkout sync/from-project-1_13-07-26or create one
git checkout -b sync/from-project-1_dd-mm-yy- Fetch latest refs from source remote:
git fetch project-1- Confirm source branch exists:
git for-each-ref --format='%(refname:short)' refs/remotes/project-12. Cherry-Pick Selected Commits (One by One)
Use this when you want only specific commits.
- List commits:
git log --oneline project-1/feat/latest-branch-name- Cherry-pick in oldest-to-newest order:
git cherry-pick <hash1>
git cherry-pick <hash2>
git cherry-pick <hash3>- If conflicts happen:
# resolve files
git add <resolved-files>
git cherry-pick --continue- Abort if needed:
git cherry-pick --abort3. Cherry-Pick All Commits From One Source Branch
There are two valid methods.
Method A: Source branch has unique commits (preferred)
Use this when source branch still has commits not yet in its upstream base.
- Compute common base and apply full range:
BASE=$(git merge-base HEAD project-1/feat/latest-branch-name)
git cherry-pick $BASE..project-1/feat/latest-branch-name- Resolve conflicts if any, then continue:
git add <resolved-files>
git cherry-pick --continueMethod B: Source branch already merged upstream
Sometimes this check returns zero:
git rev-list --count project-1/main..project-1/feat/latest-branch-nameIf result is 0, the feature branch itself has no unique commits left (it was merged already). In this case:
- Find the merge commit on upstream main:
git log --oneline --decorate --merges --grep='latest-branch-name' project-1/main- Cherry-pick that merge commit with mainline parent:
git cherry-pick -m 1 -x <merge-commit-hash>Notes:
-m 1selects parent #1 as mainline.-xappends original commit reference to message.
4. Multiple Source Branches (All, One by One)
If you need to bring all changes from several source branches into one target branch, repeat this sequence per branch:
- Identify source branch.
- Check whether it has unique commits vs its base.
- Use Method A or Method B from Section 3.
- Resolve conflicts and continue.
- Run quick validation (build/tests).
Recommended order:
- Pick foundational/backend branches first.
- Pick UI/feature branches after backend dependencies are present.
5. Conflict Resolution Workflow
During cherry-pick conflict:
git status --short- Resolve each conflicted file manually.
- Stage resolved files:
git add <file1> <file2>- Continue:
git cherry-pick --continue- If the conflict should not be included, skip current commit:
git cherry-pick --skip- If process must be cancelled:
git cherry-pick --abort6. Finalize And Push
- Confirm log and clean tree:
git status --short
git log --oneline -n 5- Push target branch:
git push -u origin sync/from-project-1_13-07-26- Open PR from target branch to main.
7. Example Session (What Was Done)
For project-1/feat/latest-branch-name into sync/from-project-1_13-07-26:
- Fetched remote refs.
- Verified branch exists.
- Detected no unique commits vs
project-1/main. - Located merge commit on
project-1/main. - Ran:
git cherry-pick -m 1 -x c15f093b187a25519abc5f33c7fc7d72c34f8c20- Resolved conflicts.
- Continued cherry-pick.
- Pushed sync branch to origin.
This pattern is the standard process when a feature branch is already merged upstream.