Advanced Operations
Power User Tools
Why it matters
These commands separate beginners from power users. Master them to handle complex situations with confidence.
Key concepts
- cherry-pick — Copy specific commits to current branch
- bisect — Binary search to find bug-introducing commit
- stash — Temporarily save uncommitted changes
- rebase -i — Interactive rebase to edit commit history
The idea
The Power Tools
cherry-pick: Copy a single commit to another branch. Like copying one chapter from one book to another.
bisect: Find which commit introduced a bug. Binary search through history—incredibly efficient.
stash: Temporarily hide changes. Like putting papers in a drawer while you clean your desk.
rebase -i: Rewrite history. Edit, squash, reorder, or drop commits before sharing.
Walkthrough
Cherry-Pick
# Copy commit abc123 to current branch
git cherry-pick abc123
# Cherry-pick without committing (just apply changes)
git cherry-pick --no-commit abc123
# Cherry-pick a range
git cherry-pick abc123..def456
Use case: Backport a bugfix from main to a release branch.
Bisect (Binary Search for Bugs)
# Start bisect session
git bisect start
# Mark current commit as bad
git bisect bad
# Mark known good commit
git bisect good v1.0
# Git checks out middle commit
# Test, then mark it:
git bisect good # or
git bisect bad
# Repeat until Git finds the culprit
# When done:
git bisect reset
Automated bisect:
git bisect run ./test-script.sh
# Returns 0 = good, 1+ = bad
Stash
# Save current changes
git stash
# Save with message
git stash push -m "Work in progress on login"
# List stashes
git stash list
# Apply most recent stash (keep in list)
git stash apply
# Apply and remove from list
git stash pop
# Apply specific stash
git stash apply stash@{2}
# Drop a stash
git stash drop stash@{0}
Interactive Rebase
# Rebase last 5 commits interactively
git rebase -i HEAD~5
Opens editor with commands:
pick abc123 First commit
pick def456 Second commit
squash ghi789 Third commit (squash into second)
reword jkl012 Fourth commit (edit message)
drop mno345 Fifth commit (remove entirely)
Commands:
pick: Keep commit as-isreword: Keep commit, edit messagesquash: Combine with previous commitfixup: Like squash but discard messagedrop: Remove commit entirelyedit: Pause to amend commit
Key takeaways
- cherry-pick for selective backporting
- bisect finds bugs in O(log n) commits
- stash saves WIP without committing
- rebase -i cleans history before sharing
Dos & don’ts
✅ DO
- Use stash for quick context switches: Don’t commit WIP
- Use bisect for mysterious regressions: Let Git find the bug
- Squash before pushing: Clean history is easier to review
- Use —fixup with rebase:
git commit --fixup abc123+git rebase -i --autosquash
❌ DON’T
- Don’t cherry-pick carelessly: Creates duplicate commits
- Don’t rebase -i pushed commits: Rewrites public history
- Don’t forget stashed changes: They expire after 90 days
Going deeper
The reflog protects you:
Even after dangerous operations, reflog keeps references for 90 days.
git reflog shows all HEAD movements. You can recover almost anything.
Autosquash workflow:
- Make fix:
git commit --fixup abc123 - Later:
git rebase -i --autosquash main - Git automatically reorders and squashes
Common mistakes
Cherry-pick conflicts: The commit may depend on earlier changes not present in target branch. You’ll need to resolve conflicts manually.
Interactive rebase on wrong branch:
Always double-check which branch you’re on before rebase -i.
git branch shows current branch with asterisk.