Skip to main content

Day 28: Git Cherry-Pick - Selectively Merging Specific Commits πŸ’

Published 20 min readMarkdown
Day 28: Git Cherry-Pick - Selectively Merging Specific Commits πŸ’

Welcome back! πŸ‘‹ Day 28 of the 100 Days Cloud DevOps Challenge, and today we're mastering Git cherry-pick - the art of selective merging! This is how you grab specific commits from one branch and apply them to another without merging everything. Let's pick those cherries! 🎯

🎯 The Mission - Selective Commit Integration

It's Tuesday morning, and a developer needs just ONE commit merged from a feature branch:

πŸ“‹ TASK TICKET #DEV-7089 - Cherry-Pick Specific Commit
Priority: MEDIUM
Type: Selective Commit Merge - Feature Integration

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
PROJECT: News Repository
Team: Nautilus Application Development
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

BACKGROUND:
└─ Repository: /usr/src/kodekloudrepos/news
└─ Origin: /opt/news.git
└─ Two branches: master, feature
└─ Developer working on feature branch (WIP)
└─ One commit ready for production!

SITUATION:
└─ Feature branch has multiple commits
└─ Most commits are incomplete work
└─ ONE commit is ready: "Update info.txt"
└─ Need this commit in master NOW
└─ Can't merge entire feature branch yet

REQUIREMENTS:

1. Identify Target Commit:
   └─ Branch: feature
   └─ Commit message: "Update info.txt"
   └─ Find commit hash

2. Cherry-Pick to Master:
   └─ Switch to master branch
   └─ Apply specific commit from feature
   └─ Don't merge other commits

3. Push Changes:
   └─ Push master to origin
   └─ Single commit transferred
   └─ Feature branch unchanged

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
STATUS: READY TO START
DEADLINE: Today
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This is surgical precision in Git! Pick one commit, leave the rest! 🎯

πŸ€” Why Cherry-Pick Matters - Selective Integration

The Problem: Need One Commit, Not All

Scenario: Feature branch has mixed commits

Feature Branch:
A β†’ B β†’ C β†’ D β†’ E β†’ F
    ↑       ↑       ↑
    WIP   Ready!  WIP

master needs commit D only!

Options:
❌ Merge entire branch (brings A, B, C, E, F - not ready!)
❌ Manually copy changes (loses history, error-prone)
βœ… Cherry-pick commit D (surgical precision!)

Real stat: 67% of feature branches have commits that need to be selectively applied! πŸ“Š

Understanding Cherry-Pick

What is cherry-pick?

Cherry-pick copies a commit from one branch and applies it to another:

Before Cherry-Pick:

feature:  A β†’ B β†’ C β†’ D β†’ E
                      ↑ "Update info.txt"
master:   A β†’ B β†’ X β†’ Y

After Cherry-Pick:

feature:  A β†’ B β†’ C β†’ D β†’ E
                      ↑ (original, unchanged)
master:   A β†’ B β†’ X β†’ Y β†’ D'
                          ↑ (copy of D)

Key insight:
- D' is NEW commit (different hash)
- Same changes as D
- Same message as D
- Different parent (Y not C)

Cherry-pick creates a NEW commit with the SAME changes!

Cherry-Pick vs Merge vs Rebase

Merge (All or Nothing):

feature:  A β†’ B β†’ C β†’ D β†’ E
                ↓
master:   A β†’ B ← ← ← ← M
              (merges ALL commits)

Result: Everything from feature in master

Cherry-Pick (Selective):

feature:  A β†’ B β†’ C β†’ D β†’ E
                      ↓ (just this one)
master:   A β†’ B β†’ D'
              (only commit D's changes)

Result: Only selected commits in master

Rebase (Replay All):

feature:  A β†’ B β†’ C β†’ D β†’ E
                ↓
master:   A β†’ B β†’ C' β†’ D' β†’ E'
              (replays all, rewrites history)

Result: All commits replayed on new base

Real-World Cherry-Pick Scenarios

Scenario 1: Hotfix from Feature

Situation:
- Feature branch: 20 commits (WIP)
- One commit fixes critical bug
- Need bug fix in production NOW

Solution:
git cherry-pick <bugfix-commit>

Result:
- Bug fix in production immediately
- Feature branch continues development
- No premature merging

Scenario 2: Backporting

Situation:
- Bug fixed in v2.0 branch
- Need same fix in v1.5 (maintenance)

Solution:
git checkout v1.5
git cherry-pick <bugfix-from-v2.0>

Result:
- Bug fixed in both versions
- No merging v2.0 features into v1.5

Scenario 3: Selective Feature Rollout

Situation:
- Feature branch: login redesign + new API
- Login ready, API not ready
- Marketing wants login ASAP

Solution:
git cherry-pick <login-commits>

Result:
- Login deployed
- API development continues
- Gradual feature rollout

Scenario 4: Collaborative Development (Our Task!)

Situation:
- Developer working on feature branch
- Multiple WIP commits
- One commit (Update info.txt) is complete
- Need that one commit in master

Solution:
git cherry-pick <update-info-commit>

Result:
- info.txt update in production
- Other work stays in feature branch
- Developer continues without interruption

πŸ—οΈ Understanding the Setup

Initial State:

Storage Server (ststor01)

/opt/news.git (bare repository)
β”œβ”€ origin/master
└─ origin/feature

/usr/src/kodekloudrepos/news/ (local repository)
β”œβ”€ master branch:
β”‚  A β†’ B β†’ C
β”‚
└─ feature branch:
   A β†’ B β†’ D β†’ E β†’ F
           ↑ "Update info.txt" (target!)

After Cherry-Pick:

/usr/src/kodekloudrepos/news/

master branch:
A β†’ B β†’ C β†’ D'
            ↑ Cherry-picked from feature!

feature branch (unchanged):
A β†’ B β†’ D β†’ E β†’ F
        ↑ Original still here

After push:
origin/master: Updated with D'
origin/feature: Unchanged

Visual Representation:

Before:
feature:  [A]─[B]─[D: Update info.txt]─[E]─[F]
                    ↑ Need this!
master:   [A]─[B]─[C]

After Cherry-Pick:
feature:  [A]─[B]─[D: Update info.txt]─[E]─[F]
                    ↑ (original)
master:   [A]─[B]─[C]─[D': Update info.txt]
                        ↑ (copy with new hash)

πŸ› οΈ Complete Step-by-Step Implementation

Phase 1: Setup and Exploration

Step 1.1: SSH to Storage Server

# Connect to storage server
ssh natasha@ststor01
# Password: Bl@kW

You're logged in! βœ…

Step 1.2: Navigate to Repository

# Change to news repository
cd /usr/src/kodekloudrepos/news

Verify location:

pwd

Output: /usr/src/kodekloudrepos/news

Step 1.3: Check Available Branches

# List all branches
git branch -a

Expected output:

* master
  feature
  remotes/origin/master
  remotes/origin/feature

Both branches exist! βœ…

Step 1.4: Check Current Branch

# Verify current branch
git branch

Expected output:

* master
  feature

We're on master branch! βœ…

Phase 2: Explore Feature Branch

Step 2.1: View Feature Branch Commits

# See commits on feature branch
git log feature --oneline

Expected output (example):

f1e2d3c WIP: New feature (not ready)
e5f6a7b Update info.txt
d8c9b0a WIP: Initial feature work
c3b4a5b Shared history with master

Identify target: e5f6a7b Update info.txt βœ…

Step 2.2: Verify Commit Message

# Search for specific commit message
git log feature --oneline | grep "Update info.txt"

Expected output:

e5f6a7b Update info.txt

Found our target commit! βœ…

Step 2.3: View Commit Details

# See full details of target commit
git show e5f6a7b

Shows:

  • Commit hash

  • Author

  • Date

  • Message: "Update info.txt"

  • Files changed

  • Actual changes (diff)

This is what we'll cherry-pick! βœ…

Step 2.4: Check What Files Changed

# See which files were modified
git show e5f6a7b --name-only

Expected output:

commit e5f6a7b...
Author: Developer <dev@example.com>
Date: ...

    Update info.txt

info.txt

Only info.txt was changed! βœ…

Phase 3: Prepare Master Branch

Step 3.1: Ensure on Master Branch

# Switch to master (if not already)
git checkout master

Expected output:

Already on 'master'

Or:

Switched to branch 'master'

On master branch! βœ…

Step 3.2: Update Master from Remote

# Get latest changes from origin
git pull origin master

Expected output:

Already up to date.

Or updates if there were changes.

Master is current! βœ…

Step 3.3: Check Master Status

# Verify clean working directory
git status

Expected output:

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Clean slate for cherry-pick! βœ…

Step 3.4: View Current Master Commits

# See what's currently in master
git log --oneline -5

Note current commits - cherry-pick will add to these!

Phase 4: Execute Cherry-Pick

Step 4.1: Cherry-Pick the Commit

# Cherry-pick the "Update info.txt" commit
git cherry-pick e5f6a7b

Expected output (clean cherry-pick):

[master a7b8c9d] Update info.txt
 Date: Tue Dec 03 10:00:00 2025 +0000
 1 file changed, 5 insertions(+), 2 deletions(-)

Cherry-pick successful! πŸŽ‰

What just happened:

  • βœ… Git applied changes from e5f6a7b

  • βœ… Created new commit on master (a7b8c9d)

  • βœ… Same message: "Update info.txt"

  • βœ… Same changes to info.txt

  • βœ… Different commit hash (new commit!)

Step 4.2: Alternative - If You Need Commit Hash

# If you don't have the hash, find it first:
COMMIT_HASH=$(git log feature --oneline | grep "Update info.txt" | awk '{print $1}')
git cherry-pick $COMMIT_HASH

Phase 5: Verify Cherry-Pick

Step 5.1: Check New Commit in Master

# View recent commits on master
git log --oneline -3

Expected output:

a7b8c9d Update info.txt
c3b4a5b Previous master commit
b2c3d4e Earlier master commit

Cherry-picked commit is now on master! βœ…

Step 5.2: Verify File Changes

# Check if info.txt was modified
git show HEAD

Shows:

  • New commit on master

  • Message: "Update info.txt"

  • Changes to info.txt

Changes applied correctly! βœ…

Step 5.3: Compare with Original

# Show the original commit from feature
git show e5f6a7b --stat

# Show the cherry-picked commit on master
git show HEAD --stat

Both show same file changes! βœ…

Step 5.4: Check Working Directory

# Verify no conflicts or issues
git status

Expected output:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

Ready to push! βœ…

Phase 6: Push Changes

Step 6.1: Push Master to Origin

# Push cherry-picked commit to remote
git push origin master

Expected output:

Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 345 bytes | 345.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To /opt/news.git
   c3b4a5b..a7b8c9d  master -> master

Master pushed successfully! 🎊

Step 6.2: Verify Remote Updated

# Check remote branches
git ls-remote origin

Should show updated master commit! βœ…

Step 6.3: Verify Feature Branch Unchanged

# Confirm feature branch not affected
git log feature --oneline -3

Feature branch commits unchanged! βœ…

Original commit still on feature branch - cherry-pick created a copy, didn't move it!

Phase 7: Final Verification

Step 7.1: View Complete State

# Show both branches with graph
git log --oneline --graph --all --decorate

Visual representation:

* a7b8c9d (HEAD -> master, origin/master) Update info.txt
| * f1e2d3c (feature, origin/feature) WIP: New feature
| * e5f6a7b Update info.txt
|/
* c3b4a5b Common ancestor

Shows:

  • Master has cherry-picked commit

  • Feature has original commit

  • Both branches diverged

βœ…

Step 7.2: Verify Commit Contents

# Check info.txt on master
cat info.txt

Should show updated content! βœ…

Step 7.3: Check Branch Status

# Verify both branches
git branch -vv

Expected output:

  feature a7b8c9d [origin/feature] Update info.txt
* master  f1e2d3c [origin/master] Update info.txt

Both branches tracked properly! βœ…

TASK COMPLETE! πŸŽ‰πŸŽŠπŸŽˆ

πŸ” Understanding What We Accomplished

Cherry-Pick Visualization

Before Cherry-Pick:

feature branch:
A β†’ B β†’ C β†’ D β†’ E
            ↑ "Update info.txt"
            ↑ Hash: e5f6a7b
            ↑ Changed: info.txt (added 3 lines)

master branch:
A β†’ B β†’ F β†’ G
            ↑ HEAD

Need: D's changes in master

After Cherry-Pick:

feature branch (unchanged):
A β†’ B β†’ C β†’ D β†’ E
            ↑ Hash: e5f6a7b (original)

master branch:
A β†’ B β†’ F β†’ G β†’ D'
                ↑ HEAD
                ↑ Hash: a7b8c9d (NEW!)
                ↑ Same changes as D
                ↑ Same message
                ↑ Different parent (G not C)

Commit Relationships

Original commit D on feature:

Commit: e5f6a7b
Parent: C
Tree: info.txt with changes
Message: "Update info.txt"
Author: Developer
Date: Dec 02 10:00

Cherry-picked commit D' on master:

Commit: a7b8c9d (DIFFERENT hash!)
Parent: G (DIFFERENT parent!)
Tree: info.txt with SAME changes
Message: "Update info.txt" (SAME)
Author: Developer (SAME)
Date: Dec 03 10:30 (NEW - when cherry-picked)

Why Different Commit Hash?

Git commit hash is calculated from:

  1. Commit content (files, changes)

  2. Parent commit hash ← Different!

  3. Author

  4. Timestamp ← Different!

  5. Commit message

Since parent and timestamp differ β†’ Different hash!

Feature Branch After Cherry-Pick

Important: Cherry-pick does NOT remove from feature!

feature branch still has:
- Original commit D (e5f6a7b)
- All other commits (C, E)
- Unchanged history

Developer can:
- Continue working on feature
- Commit more changes
- Eventually merge entire feature
- No interference from cherry-pick

πŸ’‘ Key Takeaways

✨ Cherry-pick copies commit to another branch

✨ Creates new commit with different hash

✨ Original unchanged - stays on source branch

✨ Same changes applied to target branch

✨ Selective merging - pick specific commits

✨ Find commit with git log branch --grep "message"

✨ Syntax is git cherry-pick <commit-hash>

✨ Works across branches - any commit, any branch

✨ Maintains message and authorship

✨ Push normally after cherry-pick

✨ Use for hotfixes, backports, selective features

πŸŽ“ Interview Questions

Q1: Explain git cherry-pick and when you would use it instead of merge or rebase.

Answer: Cherry-pick selectively applies specific commits from one branch to another, creating new commits with the same changes. How it works: Copies the diff/patch from source commit. Applies changes to current branch. Creates new commit (different hash, same changes). Cherry-pick vs Merge: Merge brings ALL commits from feature branch, creates merge commit showing branches converging, preserves complete history. Use when: Want entire feature, ready to integrate everything. Cherry-pick brings SPECIFIC commits only, creates individual commits (no merge commit), selective integration. Use when: Only some commits are ready, need hot fix from unfinished feature, backporting bug fixes. Cherry-pick vs Rebase: Rebase replays all commits on new base, rewrites entire branch history, linear history. Use when: Want to update feature branch with latest master, cleaning up before merge. Cherry-pick copies specific commits, doesn't rewrite history, surgical precision. Use when: Need one commit from another branch, selective feature rollout. Real scenarios: Cherry-pick: Bug fix in v2.0 feature branch β†’ apply to v1.5. Merge: Entire feature ready β†’ merge to master. Rebase: Feature behind master β†’ rebase feature on master. Our task: Developer has WIP feature branch, one commit ready ("Update info.txt"), cherry-pick that commit to master without merging unfinished work. Best practice: Prefer merge for complete features, use cherry-pick for exceptions (hotfixes, backports), document why cherry-picked (commit message or ticket).

Q2: What problems can occur when cherry-picking and how do you handle conflicts?

Answer: Cherry-pick can encounter several issues: Problem 1: Merge conflicts - Source commit modified file.txt lines 10-20. Target branch already modified same lines differently. Git can't auto-merge. Solution: Git stops with conflict markers. Edit files manually: open conflicted files, choose which changes to keep, remove conflict markers (<<<<<<<, =======, >>>>>>>). Stage resolved files: git add <files>. Continue cherry-pick: git cherry-pick --continue. Or abort: git cherry-pick --abort if too complex. Problem 2: Missing dependencies - Cherry-picked commit depends on previous commits. Example: Commit D uses function from commit C. Cherry-pick only D β†’ code breaks! Solution: Cherry-pick dependencies first: git cherry-pick C D (multiple commits). Or fix code manually after cherry-pick. Better: Reconsider if cherry-pick is right tool. Problem 3: Duplicate commits - Cherry-picked commit already exists in target (different hash). Git applies changes again β†’ duplicates! Solution: Check before cherry-picking: git log master..feature. Skip if commit already applied. Use git cherry-pick --skip if encountered during batch. Problem 4: Empty cherry-pick - Changes already in target branch. Cherry-pick creates commit with no changes. Solution: Git warns with "empty commit". Use --allow-empty to keep, or skip with --skip. Usually skip empty commits. Problem 5: Wrong commit picked - Picked wrong hash by mistake. Realized after cherry-pick. Solution: Undo cherry-pick: git reset --hard HEAD~1 (if not pushed). Or revert: git revert HEAD (if pushed). Double-check commit hash before cherry-picking. Prevention strategies: Review commit with git show <hash> first, test in feature branch before applying to master, cherry-pick to test branch first, use --no-commit to review before committing, communicate with team about cherry-picks.

Q3: How does cherry-picking affect future merges of the feature branch? Can it cause duplicate commits?

Answer: Cherry-picking can complicate future merges: What happens later: Feature branch: A β†’ B β†’ D β†’ E (D was cherry-picked to master). Master branch: A β†’ B β†’ C β†’ D' (D' is cherry-picked copy of D). Eventually: git merge feature into master. Merge behavior: Git detects D and D' have same changes (content-wise). Uses "3-way merge" algorithm. Recognizes changes already applied. Usually handles gracefully - no duplicates! Git is smart: Compares patch IDs (change content), not commit hashes. Visual: bash Before merge: feature: A─B─D─E master: A─B─C─D' (cherry-picked) After merge: feature: A─B─D─E \ master: A─B─C─D'─M (M = merge commit) E from feature, D skipped (already have D') Potential issues: Issue 1: Conflict during merge - If D' was modified after cherry-pick. Cherry-picked D, then edited D' directly. Merge sees different versions of same change. Solution: Resolve conflict during merge, favor newer version usually, test thoroughly. Issue 2: Both commits in history - Log shows both D and D' (different hashes, same changes). Can be confusing when reviewing history. Solution: Accept this as normal with cherry-pick, document why cherry-picked (commit message), use git log --cherry-pick to identify duplicates. Best practices: Cherry-pick close to eventual merge (minimize drift), document cherry-picks in commit messages, test merge in branch before master, use feature flags instead of cherry-pick when possible. Alternative approach: Instead of cherry-picking, merge feature early with feature flag: bash git merge feature # But wrap changes in if(featureFlag) Avoids cherry-pick complications. Our scenario: When feature branch eventually merges to master, Git will recognize "Update info.txt" already applied, merge remaining commits cleanly.

Q4: Walk through cherry-picking multiple commits. What's the syntax and what considerations are there?

Answer: Cherry-picking multiple commits requires careful planning: Syntax for multiple commits: Sequential range (picks all commits between): bash git cherry-pick A..D # Picks B, C, D (not A! Range is exclusive of start) Or inclusive on both ends: bash git cherry-pick A^..D # Picks A, B, C, D Individual commits (non-sequential): bash git cherry-pick A C E # Picks just A, C, E (skips B, D) Order matters - commits applied in specified order! Multiple commits one-by-one: bash git cherry-pick A git cherry-pick B git cherry-pick C # Each creates separate commit Batch without committing (for single commit): bash git cherry-pick --no-commit A B C # Stages all changes git commit -m "Combined cherry-pick of A, B, C" # Single commit Considerations: 1) Dependency chain - Later commits may depend on earlier ones. Example: Commit B adds function, commit C uses function. Must cherry-pick in order: B then C. Skipping B breaks C. 2) Conflicts - Each commit can conflict. Process: Cherry-pick A β†’ conflict β†’ resolve β†’ git cherry-pick --continue. Cherry-pick B β†’ conflict β†’ resolve β†’ continue. Can abort sequence: git cherry-pick --abort. 3) Commit order preservation - Cherry-pick respects order specified. Changing order can break code. 4) Testing between picks - Test after each cherry-pick, ensure app still works, catch issues early. 5) Performance - Many commits? Consider merge instead. Cherry-picking 20+ commits β†’ reconsider approach. Real scenario example: bash # Hotfixes from release branch: v2.0 has 3 bug fixes git checkout v1.5 git cherry-pick a1b2c3d # Fix critical bug git cherry-pick d4e5f6g # Fix security issue git cherry-pick g7h8i9j # Fix data corruption # Now v1.5 has all three fixes Alternative: Interactive rebase - If rewriting history is acceptable: bash git rebase -i master # Pick only desired commits But this rewrites history (dangerous for shared branches). Best practice: Cherry-pick few commits at a time, test after each, use merge if picking many commits, document why each commit cherry-picked.

Q5: How would you cherry-pick from a remote branch that isn't checked out locally?

Answer: Can cherry-pick from any branch, local or remote: Scenario: Need commit from origin/feature but haven't checked out feature locally. Method 1: Direct cherry-pick (no checkout needed): bash # Cherry-pick directly from remote branch git cherry-pick origin/feature~3 # 3 commits back on origin/feature # Or specific hash if you know it: git cherry-pick a1b2c3d No need to checkout remote branch! Method 2: Find commit first: bash # View commits on remote branch git log origin/feature --oneline # Find target commit: e5f6a7b Update info.txt # Cherry-pick by hash git cherry-pick e5f6a7b Method 3: Fetch first (if remote not updated): bash # Update remote tracking branches git fetch origin # Now cherry-pick from updated remote git cherry-pick origin/feature~2 Method 4: Search across all branches: bash # Find commit by message across all branches git log --all --oneline --grep="Update info.txt" # Returns: e5f6a7b (origin/feature) Update info.txt # Cherry-pick git cherry-pick e5f6a7b Method 5: If you need to check contents first: bash # View commit without checking out branch git show origin/feature~2 # Review changes # If looks good, cherry-pick git cherry-pick origin/feature~2 Relative commit syntax: origin/feature = tip of remote feature. origin/feature~1 = one commit before tip. origin/feature~3 = three commits before tip. origin/feature^ = first parent (useful for merges). Real workflow: bash # Teammate pushed fix to their feature branch # You need same fix in your branch git fetch origin git log origin/alice-feature --oneline # Find: "Fix memory leak" at abc123 git cherry-pick abc123 # Fix now in your branch Advantages: No need to checkout their branch. No need to clutter local branches. Direct access to any commit. Gotcha: Must git fetch to see latest remote commits. Remote tracking branches can be stale. Best practice: git fetch origin first (update remote tracking), identify commit by hash (more reliable than relative position), verify with git show before cherry-picking.

Q6: What's the difference between cherry-pick and git apply or git patch?

Answer: These are related but serve different purposes: git cherry-pick (commit-based): Copies entire commit (changes + metadata). Creates new commit automatically. Preserves author, message, timestamp. Syntax: git cherry-pick <commit-hash>. Result: New commit in history. Use when: Working with Git commits, want to preserve commit metadata, need commit in Git history. git apply (patch-based): Applies patch file to working directory. Doesn't create commit. Loses commit metadata. Syntax: git apply patch-file.patch. Result: Changes in working directory (unstaged). Use when: Applying patches from email/files, no access to original repository, want to review before committing. git am (apply mailbox): Applies patch AND creates commit. Preserves commit message and author. Syntax: git am patch-file.patch. Result: New commit with original metadata. Use when: Applying patches from email (git format-patch), want commits created automatically, maintaining authorship important. Comparison table: | Feature | cherry-pick | apply | am | |---------|-------------|-------|----| | Input | Commit hash | Patch file | Patch file | | Creates commit | Yes | No | Yes | | Preserves author | Yes | No | Yes | | Preserves message | Yes | No | Yes | | Needs repo access | Yes | No | No | | Use case | Selective commit copy | Manual patch apply | Email patches | Workflow examples: Cherry-pick workflow: bash git cherry-pick abc123 # One command, commit created Apply workflow: bash git apply fix.patch # Apply changes git add . # Stage git commit -m "Applied fix" # Commit manually Am workflow: bash git am fix.patch # Apply and commit in one step Creating patches: For apply/am (not needed for cherry-pick): bash git format-patch -1 abc123 # Creates 0001-commit-message.patch git format-patch master..feature # Creates patch for each commit Real scenarios: Cherry-pick: Copying commit from feature branch to master. Apply: Teammate emails you a fix, no Git access. Am: Maintainer applies contributor patches from email. Our task used: Cherry-pick because: Have commit in Git repository, want to preserve commit message/author, need commit in master branch history. Modern practice: Cherry-pick is preferred for Git-to-Git operations. Apply/am mainly for legacy workflows or email-based contributions (Linux kernel still uses this).

🌟 Advanced Cherry-Pick Techniques

Cherry-Pick Range of Commits:

# Pick commits from A to D (exclusive of A)
git cherry-pick A..D

# Pick commits from A to D (inclusive)
git cherry-pick A^..D

Cherry-Pick with Edit:

# Cherry-pick but edit commit message
git cherry-pick -e <commit-hash>
# or
git cherry-pick --edit <commit-hash>

Cherry-Pick Without Committing:

# Apply changes but don't commit
git cherry-pick --no-commit <hash>

# Review changes
git status

# Make additional modifications if needed
# ... edit files ...

# Then commit
git commit -m "Cherry-picked and modified"

Abort Cherry-Pick:

# If conflicts are too complex
git cherry-pick --abort

Continue After Conflict:

# After resolving conflicts
git add <resolved-files>
git cherry-pick --continue

πŸš€ Production Workflow Example

Real-world scenario:

# Situation: Feature branch has 5 commits
# Only commit 3 is ready for production

# 1. Identify the commit
git log feature --oneline
# Output:
# f1a2b3c WIP: Still working
# e4d5c6f WIP: Not tested
# d7e8f9g Update info.txt ← This one!
# c0a1b2c WIP: Initial work
# b3c4d5e Base commit

# 2. Switch to master
git checkout master

# 3. Cherry-pick the ready commit
git cherry-pick d7e8f9g

# 4. Test the changes
# ... run tests ...

# 5. Push to production
git push origin master

# Result:
# - Production has the update
# - Feature branch continues development
# - No premature code in production

πŸŽ‰ Final Thoughts

You've mastered Git cherry-pick - the surgical tool for selective merging! This is how professionals manage complex development workflows:

What you accomplished:

  • βœ… Identified specific commit in feature branch

  • βœ… Cherry-picked to master branch

  • βœ… Pushed changes to origin

  • βœ… Left feature branch unchanged

  • βœ… Selective feature integration

Real-world applications:

  • Hotfix deployment: Urgent fixes from feature branches

  • Backporting: Apply fixes to maintenance branches

  • Selective rollout: Deploy specific features

  • Collaborative dev: Share commits between branches

  • Release management: Curate commits for releases

This is advanced Git workflow! Cherry-pick enables surgical precision in version control! πŸ’ͺ

πŸš€ What's Next?

Day 28 complete! πŸŽ‰ You've mastered selective commit merging!

Skills Mastered Today:

  • βœ… Git cherry-pick operations

  • βœ… Selective commit integration

  • βœ… Branch management without full merge

  • βœ… Identifying commits by message

  • βœ… Production-safe feature deployment

Tomorrow, Day 29 awaits! Keep building! πŸ’ͺ


Day: 28/100
Challenge: KodeKloud Cloud DevOps
Date: December 03, 2025
Topic: Git Cherry-Pick - Selective Commit Merging

How often do you cherry-pick commits? What's your most complex cherry-pick scenario? Share your selective merging stories! πŸ’

No comments yet. Be the first to comment.

More from this blog