Undoing Changes: reset, revert & restore
Git provides safety nets to undo errors, revert previous commits, or reset the local workspace back to a clean tracking state.
1 Restoring and Undoing CLI
Shell — Undo Commands
# Discard modifications in a local file before staging
git restore index.html
# Unstage a file keeping modifications intact
git restore --staged index.html
# Create a new commit that completely reverses a previous commit ID
git revert d5e8f12
# Reset branch head back to commit history state (leaves files modified)
git reset d5e8f12
# WARNING: Hard reset completely wipes staging and files back to target commit
git reset --hard d5e8f12
2 Code Challenge
Challenge: Make some edits to a file, stage it, unstage it using
git restore --staged, modify the file again, and discard all modifications using git restore.