Git Exercises
Three short git drills - interactive rebase, bisect a regression, and recover lost work via reflog.
Three short challenges
EXAMPLE
# 1. Interactive rebase - clean up history before a PR
# Make a feature branch with messy commits
git switch -c feat/login
echo 'a' >> file.txt; git add . && git commit -m 'wip a'
echo 'b' >> file.txt; git add . && git commit -m 'wip b'
echo 'c' >> file.txt; git add . && git commit -m 'fixup typo'
echo 'd' >> file.txt; git add . && git commit -m 'wip c'
# Now interactive rebase to squash the wip commits into one clean commit
git rebase -i HEAD~4
# In the editor: change 'pick' to 'squash' for the wip lines, keep 'pick' on the first
# Save - then edit the combined message to be one clear summary
# Verify
git log --oneline -10
# 2. Bisect to find a regression
# Setup: a known good tag and a current broken HEAD
git bisect start
git bisect bad # current HEAD broken
git bisect good v1.0.0 # known good
# Git checks out a midpoint
# Run the test that fails
npm test
# If tests still fail:
git bisect bad
# If tests pass:
git bisect good
# Keep going until git says: <sha> is the first bad commit
git bisect reset
# You now have the exact commit. Fix it. Cherry-pick the fix into main.
# 3. Recover lost work via reflog
# Setup: simulate a bad reset
git switch main
git switch -c experiment
echo 'important' > work.txt; git add . && git commit -m 'important work'
git switch main
git branch -D experiment # oops - branch deleted, work seemingly gone
# Recover
git reflog | head
# Output looks like:
# abc123 HEAD@{0}: checkout: moving from experiment to main
# def456 HEAD@{1}: commit: important work
# ...
# Restore by branching from the commit
git switch -c experiment-recovered def456
git log -1
cat work.txt # back
# Stretch
# - Set rerere.enabled and rebase the same branch twice; the second time should auto-resolve
# - Use git worktree to do all three exercises in parallel without losing context
Why it matters
These three are not optional skills - they are the moments when git either saves your afternoon or eats your work. Drill bisect + reflog + interactive rebase until they are reflexes.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Discussion
Loading…