π
EPISODE 04
interactive rebase Β· cherry-pick Β· reflog Β· bisect
Advanced Git
Reach for the power tools when you need them: interactive rebase, cherry-pick, reflog for recovery, and bisect for hunting a regression.
rebase -icherry-pickreflogbisect
Duration
β± About 2 hours
Level
π Intermediate+
Prerequisite
π― git-coop-02
OUTCOME
Recover from any git mistake; find regressions quickly
What you'll learn
- 1Squash commits with interactive rebase
- 2Cherry-pick a commit onto another branch
- 3Recover deleted commits with reflog
- 4Find which commit introduced a bug with bisect
1. Interactive Rebase
bash
git rebase -i HEAD~5 # the last 5 commits
# pick abc1234 Implement feature
# squash def5678 Fix typo
# squash 901234a Fix another typo
# reword 5678abc Final touches
# drop 123abc5 Unrelated commitUse pick, squash, reword, fixup, drop, edit to reshape history before opening a PR. Only on your own branches!
2. cherry-pick
bash
git cherry-pick <hash> # apply a single commit
git cherry-pick <hash1>..<hash3> # range
git cherry-pick -x <hash> # add original SHA in messageGreat for hotfixes: cherry-pick a bug fix from main onto a release branch.
3. reflog β Time Machine
bash
git reflog # see every HEAD movement
git reset --hard HEAD@{4} # jump back to an earlier stateπ‘
If you deleted a branch or reset --hard'd by mistake, the commits live in the reflog for ~30 days. Don't panic.
4. bisect β Find the Bad Commit
bash
git bisect start
git bisect bad # current commit is bad
git bisect good v1.0.0 # this older one was good
# git checks out the midpoint β test the code
git bisect good # if it works
# OR
git bisect bad # if it fails
# repeat until git points at the offender
git bisect resetExample code / lecture materials
All lecture materials and example code are openly available on GitHub.
View on GitHub β