Interactive Rebasing, Git Reflog, and Disaster Recovery
Git almost never deletes commit data immediately. Even if you accidentally run git reset --hard HEAD~5 or delete a branch with unpushed commits, git reflog preserves all local pointer movements. This guide covers Interactive Rebasing (rebase -i), Disaster Recovery with Reflog, Cherry-Picking, and Bisect debugging.
⚡ Quick Dive
Interactive Rebase Commands Cheat Sheet (git rebase -i HEAD~4)
| Action | Command | Purpose |
|---|---|---|
pick |
pick a1b2c3d |
Keep the commit as-is |
reword |
reword a1b2c3d |
Edit the commit message |
edit |
edit a1b2c3d |
Pause rebase to amend files or split into multiple commits |
squash |
squash a1b2c3d |
Meld commit into previous commit; prompts to combine messages |
fixup |
fixup a1b2c3d |
Meld commit into previous commit; silently discards this message |
drop |
drop a1b2c3d |
Remove the commit entirely |
📖 Extended Guide
1. Disaster Recovery with git reflog
If you accidentally run git reset --hard and lose all your work:
# 1. View local reference log of every HEAD change
git reflog
# Output:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~5 (Catastrophe!)
# 9f8e7d6 HEAD@{1}: commit: Completed important payment feature (Lost commit!)
# 2. Restore exact state prior to the hard reset
git reset --hard HEAD@{1} # Or: git branch recovered-feature 9f8e7d6
2. Binary Search Debugging with git bisect
Find the exact commit that introduced a regression in $O(\log N)$ time:
git bisect start
git bisect bad # Current commit has the bug
git bisect good v1.4.0 # Last known good release
# Git checks out middle commit automatically. Run tests:
npm test # If fails: git bisect bad | If passes: git bisect good
git bisect reset # Done!