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)
2. Copy the Commit
git cherry-pick abc1234 # Copies "Fix login bug" to current branch
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
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
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!
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
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
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>
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
4. Cherry-Pick and Skip on Conflict (--abort)
Abort cherry-pick if conflicts arise and start again.
git cherry-pick <commitSHA> --abort
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>
6. Cherry-Pick Multiple Non-Sequential Commits
List multiple commits in a single command.
git cherry-pick <hash1> <hash2> <hash3>
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
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
π When Should You Use Cherry-Pick?
Perfect for:
- Fixing mistakes (Example: Accidentally committed to dev instead of main)
- Sharing small fixes (Example: Need just one bugfix from a teammateβs branch)
- 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)