Time Travel and Course Corrections
Mistakes happen. Git provides powerful tools to undo changes, from small typos to major disasters.
Undoing Unstaged Changes
Discard changes in working directory:
git restore filename.txt
# Or older Git:
git checkout -- filename.txt
Unstaging Files
git restore --staged filename.txt
# Or:
git reset HEAD filename.txt
Amending Last Commit
git commit --amend
Adds staged changes to the previous commit. Can also change the message.
Reverting a Commit
Create new commit that undoes a previous one:
git revert abc123
Safe for public history.
Resetting
Soft Reset
git reset --soft HEAD~1
Undoes commit, keeps changes staged.
Mixed Reset (Default)
git reset HEAD~1
Undoes commit, keeps changes unstaged.
Hard Reset (Dangerous!)
git reset --hard HEAD~1
Undoes commit and discards all changes. Cannot be undone!
Recovering Lost Commits
git reflog
Shows all HEAD movements. Can recover "lost" commits.
Safety Guidelines
- Never reset/rebase commits pushed to shared branches
- Use
revertfor public history --hardis destructive—use cautiously- When in doubt, create a backup branch first
Next: Boarding Other Vessels
You can now fix mistakes. Next, learn to clone repositories—downloading complete projects from GitHub.