Undo and Fix
Undo changes
Section titled “Undo changes”git checkout -- file.txt # discard uncommitted changes
Undo the last commit
Section titled “Undo the last commit”git reset --soft HEAD~1 # keep files stagedgit reset --hard HEAD~1 # completely remove changes
Roll back cleanly
Section titled “Roll back cleanly”git revert abc123
Stash changes
Section titled “Stash changes”git stashgit stash apply
Best practices
Section titled “Best practices”- Use
git reset
only locally (dangerous if shared). - Prefer
git revert
to undo changes in a collaborative project. - Use
stash
to pause ongoing work.