Git Cherry-Pick

1 min

Table of contents

  1. What Is Cherry-Pick?
  2. Quick Practice
  3. Usage Scenario
  4. Essential Commands

1. What Is Cherry-Pick?

Git cherry-pick = Copy ONE specific commit from one branch to another.

Concrete example:

  • On feature-login: a critical bug-fix commit
  • On main: you want JUST that fix, not the whole branch
  • Solution: git cherry-pick <commit-hash>

Back to table of contents

2. Quick Practice

Creating the project:

bash
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.

Back to table of contents

3. Usage Scenario

Typical situation:

  1. Development on feature-branch
  2. Critical bug discovered and fixed
  3. Production (main) needs the fix NOW
  4. But not the whole feature in development

Solution:

bash
git log --oneline feature-branch    # Find the fix hash
git checkout main
git cherry-pick abc123f             # Apply JUST that commit

Back to table of contents

4. Essential Commands

CommandAction
git cherry-pick <hash>Copy a commit
git cherry-pick <hash1> <hash2>Copy several commits
git cherry-pick --continueContinue after conflict resolution
git cherry-pick --abortCancel the operation

That's all! Cherry-pick is simple: 1 commit → 1 copy.

Back to table of contents