← Back to Mission Control

Mission Corrections

2 min read

Undoing in Git

Mission Phase 23 • Difficulty: Intermediate

Time Travel and Course Corrections

Mistakes happen. Git provides powerful tools to undo changes, from small typos to major course corrections.

Undoing Unstaged Changes

Discard changes in your 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 the Last Commit

git commit --amend

This adds staged changes to the previous commit and can also change the commit message.

Reverting a Commit

Create a new commit that undoes a previous one:

git revert abc123

This is safe for public history because it does not rewrite existing commits.

Resetting

Soft Reset

git reset --soft HEAD~1

Undoes the commit and keeps the changes staged.

Mixed Reset (Default)

git reset HEAD~1

Undoes the commit and keeps the changes unstaged.

Hard Reset (Dangerous!)

git reset --hard HEAD~1

Undoes the commit and discards tracked working-tree changes. Use with extreme caution.

Recovering Lost Commits

git reflog

Shows all HEAD movements and can help recover "lost" commits.

Safety Guidelines

  • Never reset or rebase commits pushed to shared branches
  • Use revert for public history
  • --hard is destructive—use cautiously
  • When in doubt, create a backup branch first

Next: Timeline Rewriting

You can now fix mistakes. Next, learn rebasing—an advanced but powerful technique for maintaining clean history.