Files
2026-01-07 17:59:02 +01:00
..
2026-01-07 17:59:02 +01:00
2026-01-07 17:59:02 +01:00
2026-01-07 17:59:02 +01:00
2026-01-07 17:59:02 +01:00

Module 09: Cherry-Pick

Learning Objectives

By the end of this module, you will:

  • Understand what cherry-picking is and how it works
  • Know when to use cherry-pick vs merge or rebase
  • Apply specific commits from one branch to another
  • Handle cherry-pick conflicts if they occur
  • Understand common use cases for cherry-picking

Challenge Description

You have a development branch with several commits. Some of these commits are bug fixes that need to be applied to the main branch immediately, but other commits are experimental features that shouldn't be merged yet.

Your task is to:

  1. Review the commits on the development branch
  2. Identify which commits are bug fixes
  3. Cherry-pick only the bug fix commits to the main branch
  4. Verify that main has the bug fixes but not the experimental features

Key Concepts

What is Cherry-Pick?

Cherry-pick allows you to apply a specific commit from one branch to another. Instead of merging an entire branch, you can selectively choose individual commits.

    A---B---C---D development
   /
  E---F main

After cherry-picking commit C:

    A---B---C---D development
   /
  E---F---C' main

Note that C' is a new commit with the same changes as C but a different commit hash.

Cherry-Pick vs Merge vs Rebase

  • Merge: Brings all commits from another branch and creates a merge commit
  • Rebase: Replays all commits from your branch on top of another branch
  • Cherry-Pick: Applies one or more specific commits to your current branch

When to Use Cherry-Pick

Cherry-pick is useful when you:

  • Need a bug fix from a feature branch but can't merge the whole branch yet
  • Want to apply a specific commit to a release branch
  • Need to backport a fix to an older version
  • Made a commit on the wrong branch and need to move it
  • Want to duplicate a commit across multiple branches

Cherry-Pick Creates New Commits

Important: Cherry-picked commits are new commits with different hashes. The original commit remains on the source branch, and a copy is created on the target branch with the same changes but a different commit ID.

Useful Commands

# View commits on another branch
git log <branch-name> --oneline

# View a specific commit's details
git show <commit-hash>

# Cherry-pick a single commit
git cherry-pick <commit-hash>

# Cherry-pick multiple commits
git cherry-pick <commit-hash1> <commit-hash2>

# Cherry-pick a range of commits
git cherry-pick <start-hash>..<end-hash>

# If conflicts occur during cherry-pick:
# 1. Resolve conflicts in files
# 2. Stage the resolved files
git add <file>
# 3. Continue the cherry-pick
git cherry-pick --continue

# Abort a cherry-pick if something goes wrong
git cherry-pick --abort

# View commit history graph
git log --oneline --graph --all

Verification

Run the verification script to check your solution:

.\verify.ps1

The verification will check that:

  • You're on the main branch
  • The security bug fix commit has been applied to main
  • The performance bug fix commit has been applied to main
  • The experimental features are NOT on main
  • The commits were cherry-picked (not merged)

Challenge Steps

  1. Navigate to the challenge directory
  2. You're currently on the development branch
  3. View the commits: git log --oneline
  4. You'll see several commits - identify the bug fixes
  5. Switch to main branch: git switch main
  6. Cherry-pick the bug fix commits (you'll need their commit hashes)
  7. Verify the result with git log --oneline
  8. Run the verification script

Tips

  • Use git log development --oneline to see commits on the development branch
  • Use git show <hash> to view details of a specific commit
  • You can cherry-pick by commit hash - you only need the first 7 characters
  • Cherry-pick commits in chronological order (oldest first) to avoid conflicts
  • If you make a mistake, use .\reset.ps1 to start over
  • The commit message will be preserved when cherry-picking

Common Cherry-Pick Scenarios

Hotfix to Production

You have a critical bug fix on a development branch that needs to go to production immediately:

git switch production
git cherry-pick <bugfix-commit-hash>

Wrong Branch

You accidentally committed on the wrong branch:

# On wrong branch, note the commit hash
git log --oneline
# Switch to correct branch
git switch correct-branch
git cherry-pick <commit-hash>
# Go back and remove from wrong branch
git switch wrong-branch
git reset --hard HEAD~1

Backporting

You need to apply a fix to an older release branch:

git switch release-2.0
git cherry-pick <fix-from-main>

What You'll Learn

Cherry-pick is a surgical tool in your Git toolbox. While merge and rebase work with entire branches, cherry-pick lets you be selective about which changes to apply. This is invaluable for managing hotfixes, maintaining multiple release branches, and handling situations where you need specific changes without bringing along everything else. Understanding when to use cherry-pick versus other Git operations is a mark of Git expertise.