Git cherry-pick = Copy ONE specific commit from one branch to another.
Concrete example:
feature-login: a critical bug-fix commitmain: you want JUST that fix, not the whole branchgit cherry-pick <commit-hash>Creating the project:
mkdir demo-cherry-pick && cd demo-cherry-pick
git init
echo "print('v1')" > app.py
git add . && git commit -m "Initial version"
# Create a feature branch
git checkout -b feature-new
echo "print('v2-feature')" > app.py
git add . && git commit -m "New feature"
echo "print('v2-bugfix')" > app.py
git add . && git commit -m "Critical fix"
# Go back to main and cherry-pick ONLY the bugfix
git checkout main
git cherry-pick <hash-du-commit-bugfix>There you go! The fix is on main, without the feature.
Typical situation:
feature-branchmain) needs the fix NOWSolution:
git log --oneline feature-branch # Find the fix hash
git checkout main
git cherry-pick abc123f # Apply JUST that commit| Command | Action |
|---|---|
git cherry-pick <hash> | Copy a commit |
git cherry-pick <hash1> <hash2> | Copy several commits |
git cherry-pick --continue | Continue after conflict resolution |
git cherry-pick --abort | Cancel the operation |
That's all! Cherry-pick is simple: 1 commit → 1 copy.