Drill: Cherry-Pick Specific Commits Between Branches¶
Goal¶
Use git cherry-pick to selectively apply specific commits from one branch to another without merging all changes.
Setup¶
- A git repository with at least two branches
- Commits on one branch that need to be applied to another
Commands¶
Set up a practice scenario:
mkdir /tmp/cherry-drill && cd /tmp/cherry-drill && git init
echo "base" > file.txt && git add file.txt && git commit -m "base"
git checkout -b feature
echo "feature-1" >> file.txt && git add file.txt && git commit -m "feature: add part 1"
echo "feature-2" >> file.txt && git add file.txt && git commit -m "feature: add part 2"
echo "hotfix" >> file.txt && git add file.txt && git commit -m "fix: critical bugfix"
echo "feature-3" >> file.txt && git add file.txt && git commit -m "feature: add part 3"
Find the commit to cherry-pick:
Switch to main and cherry-pick the bugfix:
Cherry-pick multiple commits:
Cherry-pick a range of commits:
Cherry-pick without committing (stage only):
If there is a conflict, resolve and continue:
Abort a cherry-pick in progress:
What to Look For¶
- Cherry-picked commits get new hashes (they are new commits with the same diff)
- Conflicts arise when the cherry-picked change touches code that differs between branches
--no-commitlets you combine multiple cherry-picks into one commitgit log --cherry-markshows which commits have been cherry-picked between branches
Common Mistakes¶
- Cherry-picking a commit that depends on earlier commits not present on the target branch
- Forgetting that cherry-pick creates duplicate commits (later merge may show conflicts)
- Not using
--no-commitwhen you want to squash multiple picks into one - Cherry-picking merge commits without specifying
-m 1for the mainline parent