undoing things

undoing things

reset, restore, revert — how to get out of a mess

undoing things

copy
git reset --hard HEAD~1

nukes the last commit and all its changes. gone. use with caution.

copy
git reset --soft HEAD~1

removes the last commit but keeps the changes staged. useful if you committed too early and want to rework things.

copy
git restore src/some-file.ts

discards unstaged changes to a file. brings it back to what it was at HEAD. can't undo this so be sure.

copy
git restore --staged src/some-file.ts

unstages a file without throwing away your changes. it's just removed from the upcoming commit.

copy
git revert abc1234

creates a new commit that undoes a previous one. this is the safe option if the commit has already been pushed to a shared branch — it doesn't rewrite history.