This file documents the Git undo and recovery tools I've learned.
- git reset --soft HEAD~1: undo last commit, keep changes staged
- git reset --mixed HEAD~1: undo last commit, keep changes in working directory (default)
- git reset --hard HEAD~1: undo last commit and discard all changes (dangerous!)
- Only safely use reset on commits that haven't been pushed
- git revert HEAD: create a new commit that undoes the last commit
- git revert is safe for shared/pushed branches because it doesn't rewrite history
- The original commit stays in the log, plus a new "undo" commit is added
- git reflog: shows everywhere HEAD has pointed (commits, resets, checkouts)
- Reflog entries last about 90 days before being garbage collected
- To recover: find the SHA in reflog, then git branch
- git cherry-pick : apply a specific commit to the current branch
- Creates a new commit with the same changes but a different SHA
- Use for hotfixes: fix on feature branch, cherry-pick to main
- git bisect start: begin a binary search session
- git bisect bad: mark current commit as containing the bug
- git bisect good : mark a known-good commit
- Git checks out middle commits; you test and mark good/bad
- git bisect reset: end the session and return to original HEAD
- git tag -a v1.0 -m "message": create an annotated tag (stores tagger, date, message)
- git tag v1.0: create a lightweight tag (just a pointer, no metadata)
- git push origin v1.0: push a specific tag to remote
- git push --tags: push all tags
- Annotated tags are for releases; lightweight tags are for private/temporary labels
- CI/CD pipelines often trigger on new tags