Cherry-Picking

Advanced Git

Amanda Crawford-Adamo

Software and Data Engineer

What is cherry-pick?

Applies the changes from a specific commit to another branch

Cherry-Pick Single Commit

git cherry-pick <commit-hash>

Cherry-Pick Multiple Commits

git cherry-pick <hash1> <hash2> ..

Purpose

  • Apply specific bug fixes across branches
  • Roll back features to stable versions
  • Selectively apply experimental changes
  • Recover lost commits
Advanced Git

Cherry-Pick example

A graph of both main and feature. Main has two commit. The feature branch diverges from main after the last commit.

Two images showing main and feature branch log before the cherry pick process.

Advanced Git

Cherry-Pick process

  1. Checkout main branch

    git checkout main
    
  2. Run cherry-pick command to bring in commit def456 changes from feature branch.

    git cherry-pick def456
    

A graph of both main and feature. Main has three commit. The feature branch diverges from main after the second commit with two additional. commit. Main's third commit is a cherry picked commit from feature branch's last commit.

Advanced Git

Resolving cherry-pick conflicts

Conflict resolution steps
  1. Manually edit conflicting files
  2. Add resolved files to staging using git add <resolved-files>
  3. Continue with the cherry-pick process by running --continue flag
git cherry-pick --continue
Stopping a cherry-pick

To stop a cherry-pick operation, use the --abort flag

git cherry-pick --abort
Advanced Git

When to use cherry-pick

Use cases

  1. Applying hotfixes
  2. Testing isolated features

Cautions

  1. Can create duplicate commits
  2. May complicate project history if overused
  3. Larger changes: consider merging or rebasing
Advanced Git

Let's practice!

Advanced Git

Preparing Video For Download...