Last Updated: May 1, 2025
Reflog Recovery
git reflogShow history of all HEAD movements (local, 90 days)
git reflog show branchShow reflog for a specific branch
git checkout HEAD@{2}Check out where HEAD was 2 moves ago
git branch recovered HEAD@{1}Create a branch pointing to a lost commit
git reflog expire --expire=now --allClear reflog entries (use with caution!)
git fsck --lost-foundFind dangling commits not in any branch
Bisect Debugging
git bisect startBegin binary search for bug-introducing commit
git bisect bad HEADMark current commit as bad (has the bug)
git bisect good v1.0Mark known-good commit (before bug appeared)
git bisect goodMark current checked-out commit as good
git bisect run ./test.shFully automated bisect — script returns 0=good
git bisect logShow bisect progress log
git bisect replay logfileReplay a previous bisect session
git bisect resetEnd bisect and return to original HEAD
Worktrees
git worktree add ../path branchCreate a new working directory for a branch
git worktree listList all worktrees with their branches and paths
git worktree remove ../pathRemove a worktree (clean up after done)
git worktree prunePrune worktree entries for deleted directories
git worktree add -b new-branch ../pathCreate worktree with a new branch
cd ../pathSwitch context without stashing — ideal for hotfixes
Hooks & Filter-Branch
nano .git/hooks/pre-commitEdit the pre-commit hook (add linting, tests)
chmod +x .git/hooks/pre-commitMake hook executable (required!)
git filter-branch --tree-filter cmdRewrite history applying command to each commit
git filter-repo --path file --invert-pathsModern alternative: remove file from history
git format-patch -3 HEADGenerate .patch files for last 3 commits
git am *.patchApply patches from mailbox (preserves authorship)
Pro Tip: The reflog is your safety net — it records every HEAD movement for 90 days. If you lose commits, `git reflog` will find them.