Skip to content

Latest commit

 

History

History
314 lines (198 loc) · 4.4 KB

File metadata and controls

314 lines (198 loc) · 4.4 KB

🍒 Git Cherry-Pick (Selective Commit Transfer)

Apply specific commits from one branch to another without merging everything.


📌 What Is Git Cherry-Pick?

git cherry-pick allows you to:

Take a specific commit from one branch and apply it to another branch.


🧠 Why Cherry-Pick?

Scenario:

You fixed a bug in develop
But production needs the same fix urgently

Instead of merging entire branch:

👉 Use cherry-pick to bring ONLY that commit


🗺️ Big Picture

flowchart LR
    A[Branch A] --> B[Commit X]
    B --> C[Cherry Pick]
    C --> D[Branch B]
Loading

🧬 Visual Example

main:     A --- B --- C
                    \
feature:             D --- E (fix commit)

Cherry-pick E → main

Result:
main:     A --- B --- C --- E'

👉 E' = new commit (copy of E)


🧱 Basic Command

git cherry-pick <commit-hash>

🧠 Important Concept

Cherry-pick does NOT move commit.

It creates a new commit with same changes.


🔍 How to Find Commit Hash

git log

🧪 Real-World Scenario

1. Bug fixed in feature branch
2. Production needs fix
3. Cherry-pick commit
4. Deploy fix

🧱 Step-by-Step Example


Step 1 — Go to target branch

git checkout main

Step 2 — Cherry-pick commit

git cherry-pick abc123

Result

New commit added to main

🔄 Multiple Commits

git cherry-pick commit1 commit2 commit3

🔁 Range of Commits

git cherry-pick A^..D

⚠️ Cherry-Pick Conflicts

If changes overlap:

git cherry-pick abc123

May result in:

CONFLICT (content): Merge conflict

Fix Conflict

# resolve manually
git add .
git cherry-pick --continue

🧠 Internal Behavior

1. Git takes commit changes
2. Applies diff to current branch
3. Creates new commit

🧬 Internal Flow

Commit (source branch)
        ↓
Diff extracted
        ↓
Applied to target branch
        ↓
New commit created

🔀 Cherry-Pick vs Merge

Cherry-Pick Merge
specific commits full branch
creates new commit combines history
precise broader

🔀 Cherry-Pick vs Rebase

Cherry-Pick Rebase
select commits replay full branch
manual automatic sequence

🚨 Common Mistakes


❌ Duplicate commits

Cherry-pick creates new commit → duplicates possible.


❌ Losing context

Commit may depend on earlier commits.


❌ Overusing cherry-pick

Can create messy history.


✅ Best Practices

  • use for hotfixes
  • use for small changes
  • avoid large features
  • understand dependencies
  • test after applying

🧪 Real Use Cases

  • hotfix in production
  • applying patch across branches
  • backporting changes
  • selective bug fixes

🎤 Interview Questions

What is git cherry-pick?

Apply a specific commit from one branch to another.


Does cherry-pick move commit?

No, it copies changes and creates new commit.


When to use cherry-pick?

For selective changes, hotfixes, or backports.


What happens if conflict occurs?

Resolve manually and continue.


🧪 Practice Lab

# create commit in branch
git checkout -b feature
echo "fix" >> file.txt
git commit -am "Fix bug"

# go to main
git checkout main

# cherry-pick
git cherry-pick <commit-hash>

🎯 Final Takeaway

Cherry-pick is a precision tool.

Use it when you need:

  • exact change
  • quick fix
  • minimal impact

👉 Next Step

➡️ 03-git-reflog.md