Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Git

Git: Use Cherry-Pick to Apply Specific Commits Across Branches

- 30.03.26 - ErcanOPAK

🍒 Pick Commits Like Cherries

Need one commit from another branch? Don’t merge everything. Cherry-pick copies specific commits to your current branch. Surgical precision.

The Problem

# Scenario: Bug fix on wrong branch
git branch
* feature-login
  main
  hotfix

# You committed bug fix to feature-login
# But it needs to be on hotfix branch too!

git log --oneline
a1b2c3d Fix critical security bug  ← Need this on hotfix!
d4e5f6g Add login form
g7h8i9j Update styles

# Don't want to merge entire feature branch
# Just need the security fix

Cherry-Pick Solution

# Switch to target branch
git checkout hotfix

# Cherry-pick the commit
git cherry-pick a1b2c3d

# Done! Commit copied to hotfix branch
git log --oneline
a1b2c3d Fix critical security bug  ← Same commit, now on hotfix!
x7y8z9a Previous hotfix commit

🎯 Common Use Cases

1. Bug Fix on Wrong Branch
# Fixed bug on feature branch, need on main
git checkout main
git cherry-pick abc123

# Or cherry-pick from another person's branch
git cherry-pick origin/their-branch-name~2  # 2 commits ago
2. Apply Multiple Commits
# Cherry-pick range of commits
git cherry-pick abc123..def456

# Cherry-pick multiple specific commits
git cherry-pick abc123 def456 ghi789

# Cherry-pick all commits from another branch
git cherry-pick feature-branch
3. Hotfix to Multiple Versions
# Apply same fix to v1.0, v2.0, v3.0 branches
git checkout v1.0-stable
git cherry-pick abc123

git checkout v2.0-stable
git cherry-pick abc123

git checkout v3.0-stable
git cherry-pick abc123

# Same fix on all versions!

Handle Conflicts

git cherry-pick abc123

# If conflicts:
# CONFLICT (content): Merge conflict in file.js

# Fix conflicts
vim file.js  # Resolve conflicts

# Mark as resolved
git add file.js

# Continue cherry-pick
git cherry-pick --continue

# Or abort
git cherry-pick --abort

🔧 Advanced Options

# Cherry-pick without committing (stage changes only)
git cherry-pick -n abc123
# Make modifications, then commit manually

# Cherry-pick and edit commit message
git cherry-pick -e abc123

# Add 'cherry picked from' note to commit message
git cherry-pick -x abc123
# Adds: (cherry picked from commit abc123)

# Cherry-pick but with different author
git cherry-pick abc123
git commit --amend --author="John Doe "

# Cherry-pick only the changes, not the commit
git cherry-pick -n abc123
git reset  # Unstage
# Now you have changes in working directory

Real-World Workflow

📋 Production Hotfix Workflow

# Bug found in production!
# Create hotfix branch from main
git checkout main
git checkout -b hotfix-critical-bug

# Fix bug, commit
git commit -m "Fix critical login bug"
# Commit hash: xyz789

# Deploy hotfix to production
git checkout main
git merge hotfix-critical-bug
git push

# Now cherry-pick to development branch
git checkout develop
git cherry-pick xyz789

# Now cherry-pick to all active feature branches
git checkout feature-new-ui
git cherry-pick xyz789

git checkout feature-api-v2
git cherry-pick xyz789

# Same fix everywhere!

✅ When to Use Cherry-Pick

  • Bug fixes: Apply to multiple branches/versions
  • Backporting: Bring new features to old versions
  • Mistakes: Commit on wrong branch, move it
  • Selective merging: Want some commits, not all
  • Team collaboration: Grab colleague’s commit

⚠️ Caveats

  • Creates duplicate commits: Same change, different hash
  • Can cause conflicts: If code has diverged
  • Loses context: Commit might not make sense alone
  • Tracking is harder: Git doesn’t track cherry-picks automatically
  • Prefer merge when possible: Cherry-pick is for exceptions

💡 Pro Tips

# Find commits to cherry-pick
git log --oneline --graph branch-name

# Cherry-pick from reflog (even deleted commits!)
git reflog
git cherry-pick abc123

# Test before cherry-picking
git show abc123  # View commit changes

# Cherry-pick merge commit (requires -m)
git cherry-pick -m 1 merge-commit-hash
# -m 1 means use first parent

# Undo cherry-pick
git reset --hard HEAD~1  # If not pushed
git revert commit-hash   # If already pushed

🎯 Cheat Sheet

Command Action
git cherry-pick abc123 Apply single commit
git cherry-pick abc..def Apply range of commits
git cherry-pick -n abc123 Don’t commit (stage only)
git cherry-pick -x abc123 Add source note to message
git cherry-pick --continue Continue after conflict
git cherry-pick --abort Cancel cherry-pick

“Critical security fix needed in 3 production versions simultaneously. Cherry-picked same commit to v1, v2, v3 branches. All versions patched in 10 minutes. Cherry-pick saved the day.”

— DevOps Engineer

Related posts:

Why git pull Is Dangerous in Long-Lived Branches

Why git pull Is Dangerous in Long-Lived Branches

Git: Use Git Bisect to Find Which Commit Introduced a Bug

Git: Use Git Bisect to Find Which Commit Introduced a Bug

Git: Use Rebase for Clean Commit History

Git: Use Rebase for Clean Commit History

Views: 14

Post navigation

Git: Use Interactive Rebase to Clean Up Commit History Before Merge
.NET Core: Use Minimal APIs for Lightweight HTTP Services

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (608)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (546)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com