Git Advanced Tutorial: Rebasing, Cherry-Pick, and More
Key Insight
Master interactive rebase for clean history, cherry-pick for selective commits, bisect for finding bugs, and reflog for recovery. These advanced techniques separate professional Git usage from basic operations.
Introduction
Basic Git commands (add, commit, push) handle daily work. Advanced Git techniques solve complex problems and maintain clean, professional repositories.
Interactive Rebase
Interactive rebase rewrites commit history. Use it to clean up before merging to main.
Run: git rebase -i HEAD~5
Interactive Commands
- pick - Keep commit as-is
- reword - Change commit message
- squash - Combine with previous commit
- fixup - Squash and discard message
- drop - Remove commit
Safety Rules
- Never rebase shared/pushed commits
- Use --force-with-lease if you must push rebased branches
- Always have a backup branch
Cherry-Pick
Apply specific commits from one branch to another.
Run: git cherry-pick abc123
Use Cases
- Backporting bug fixes to release branches
- Pulling specific features without full merge
- Recovering commits from deleted branches
Git Bisect
Find which commit introduced a bug using binary search.
- Start: git bisect start
- Mark current (buggy) as bad: git bisect bad
- Mark known good commit: git bisect good abc123
- Git checks out middle commit
- Test and mark good or bad
- Repeat until found
- Reset: git bisect reset
Git Stash
Save uncommitted changes temporarily.
- git stash - Save changes
- git stash list - List stashes
- git stash pop - Apply and remove
- git stash apply - Apply and keep
- git stash -u - Include untracked files
Git Reflog
Reflog tracks all HEAD changes—your safety net.
Run: git reflog
Recovery Examples
- Undo accidental reset: git reset --hard HEAD@{1}
- Recover deleted branch: git checkout -b recovered HEAD@{5}
- Find lost commits: git reflog | grep commit
Conclusion
Advanced Git techniques take practice but dramatically improve your workflow. Start with interactive rebase and stash—they solve daily problems. Add bisect and reflog when you need them. Master these tools and you will handle any Git situation confidently.
Key Takeaways
- Interactive rebase cleans up messy commit history
- Cherry-pick applies specific commits to other branches
- Bisect finds which commit introduced a bug
- Reflog recovers lost commits and branches
- Stash saves work-in-progress without committing
Frequently Asked Questions
Should I rebase or merge?
Rebase for feature branches to maintain clean history. Merge for shared branches or when preserving branch history matters. Never rebase commits that others have pulled.
How do I undo a rebase?
Use git reflog to find the commit before rebase, then git reset --hard to that commit. Reflog keeps history of all HEAD changes for recovery.