Git Advanced (Rebase, Bisect, Worktree)
Beyond add/commit/push — rewrite history cleanly, hunt a bug with binary search, and juggle branches without stashing.
Rebase & rewrite
- Replay onto main
git rebase main— linear history, no merge commit- Interactive cleanup
git rebase -i HEAD~5— squash/reword/reorder/drop commits- Amend last commit
git commit --amend(message or add staged changes)- Autosquash
git commit --fixup <sha>thengit rebase -i --autosquash- Abort / continue
git rebase --abort· after fixing:--continue- Golden rule
- Never rebase commits you’ve pushed and shared — it rewrites hashes.
Find & fix
- Bisect (binary-search a bug)
git bisect start·git bisect bad·git bisect good <sha>- Automate bisect
git bisect run npm test— finds the breaking commit for you- Who changed this line
git blame -L 10,20 file- Search history for text
git log -S "functionName" --oneline(pickaxe)- Recover lost commits
git reflog— every HEAD move;git reset --hard <sha>back to it
Move work around
- Cherry-pick a commit
git cherry-pick <sha>— copy one commit here- Stash
git stash·git stash pop· list:git stash list- Worktree (branch in a 2nd dir)
git worktree add ../hotfix hotfix— no stashing, both checked out at once- Remove worktree
git worktree remove ../hotfix
Undo safely
- Unstage
git restore --staged file- Discard working changes
git restore file(gone!)- Undo a pushed commit
git revert <sha>— new commit that inverts it (safe for shared)- Soft reset (keep changes)
git reset --soft HEAD~1— uncommit, keep staged