DEV Community

Cover image for Day 2/30: Advanced Git - When and How to Use Cherry-Pick
Ruqaiya Beguwala
Ruqaiya Beguwala

Posted on • Originally published at Medium

Day 2/30: Advanced Git - When and How to Use Cherry-Pick

Welcome to Day 2 of "30 Days of Advanced Git Commands You Actually Need!"

🌱 What is Git Cherry-Pick? (For Beginners)

Imagine you wrote a great piece of code in one branch, but now need it in another branch. Instead of copying code manually or merging whole branches, git cherry-pick lets you:

  • Pluck one specific commit from anywhere in your repo
  • Apply it cleanly to your current branch
  • Keep the original commit message (or edit it)

πŸ‘‰ Think of it like copy-pasting a single commit!

πŸ› οΈ How to Cherry-Pick - Step by Step

1. Find Your Commit

First, locate the commit hash you want to copy:

git log --oneline --all
# Shows compact commit history like:
# abc1234 Fix login bug (main)
# def5678 Add profile page (feature)
Enter fullscreen mode Exit fullscreen mode

2. Copy the Commit

git cherry-pick abc1234  # Copies "Fix login bug" to current branch
Enter fullscreen mode Exit fullscreen mode

3. Resolve Conflicts (If Any)

If Git shows conflicts:

  • Open the conflicted files
  • Fix the code (look for <<<<<<< markers)
  • Run:
git add . # Mark conflicts as resolved 
git cherry-pick --continue # Finish the operation
Enter fullscreen mode Exit fullscreen mode

Real-World Use Cases

Scenario 1: Backport a Hotfix

Have a fix in main branch but want that commit in lower environments? Use this:

# On production branch (v1.0):
git cherry-pick abc123  # Applies bugfix from main to Development
Enter fullscreen mode Exit fullscreen mode

Scenario 2: Revive a Lost Commit

Accidentally deleted a branch but need a commit? Use this:

git reflog  # Find the deleted commit hash
git cherry-pick dead456  # Revive it!
Enter fullscreen mode Exit fullscreen mode

Scenario 3: Split a PR into Logical Chunks

Have a commit with different functionality combined? Split it using below command:

git cherry-pick xyz789~..xyz789  # Range of commits
Enter fullscreen mode Exit fullscreen mode

Note: You can read more about commit-ranges here.


πŸ’Ž Pro Tips (Most Guides Miss These)

1. Cherry-Pick Without Committing (--no-commit)

Stages changes but doesn’t auto-commit, allowing modifications.

git cherry-pick -n <hash>  # Stages changes without committing
git commit -m "New message"  # Customize before finalizing
Enter fullscreen mode Exit fullscreen mode

2. Cherry-Pick with Conflict Resolution Strategy (-X)

Pass merge strategy options (e.g., theirs or ours) for conflicts.

git cherry-pick -X theirs <commit-hash>
Enter fullscreen mode Exit fullscreen mode

3. Cherry-Pick from Another Branch Without Switching

Reference commits directly from another branch without checking out.

git cherry-pick main~2  # Picks the 2nd last commit from main
Enter fullscreen mode Exit fullscreen mode

4. Cherry-Pick and Skip on Conflict (--abort)

Abort cherry-pick if conflicts arise and start again.

git cherry-pick <commitSHA> --abort
Enter fullscreen mode Exit fullscreen mode

5. Cherry-Pick a Merge Commit (-m 1)

Pick one parent of a merge commit (e.g., -m 1 for mainline). Can be used for extracting changes from a specific side of a merge.

git cherry-pick -m 1 <merge-commit-hash>
Enter fullscreen mode Exit fullscreen mode

6. Cherry-Pick Multiple Non-Sequential Commits

List multiple commits in a single command.

git cherry-pick <hash1> <hash2> <hash3>
Enter fullscreen mode Exit fullscreen mode

7. Maintain clean history (--ff)

Want to avoid duplicate commits, clean the branch history using --ff.

git cherry-pick --ff <hash>  # Fast-forward if possible
Enter fullscreen mode Exit fullscreen mode

Want to read more about Fast-forward? Check out more here.

8. Bonus: Dry Run Cherry-Pick (--no-commit + diff)

Preview changes without applying them.

git cherry-pick --no-commit <hash> && git diff --cached
Enter fullscreen mode Exit fullscreen mode

πŸ’ When Should You Use Cherry-Pick?

Perfect for:

  1. Fixing mistakes (Example: Accidentally committed to dev instead of main)
  2. Sharing small fixes (Example: Need just one bugfix from a teammate’s branch)
  3. Recovering lost work (Example: Found a good commit in a deleted branch)

🚫 Don’t use it for:

  • Moving lots of commits (use merge or rebase instead)
  • Public/shared branches (can cause confusion)

Up Next: Day 3: git reflog – Your Time Machine for Lost Commits.


Daily advance GIT tips in your inboxβ€”worth starting? Respond to my poll hereπŸš€
For more useful and innovative tips and tricks, Let's connect on Medium

Top comments (0)