Git gives you tools to correct mistakes and reshape history (with care).

  1. Discard unstaged changes to a file
  • git restore path/to/file
  1. Unstage a file
  • git restore --staged path/to/file
  1. Amend the last commit (message or staged changes)
  • git commit --amend
  1. Move branch pointer (use with care)
  • git reset --soft HEAD~1 # keep index and working tree
  • git reset --mixed HEAD~1 # default; keep working tree
  • git reset --hard HEAD~1 # discards changes
  1. Revert a commit (safe on shared branches)
  • git revert <commitSHA>

Checklist

  • You can restore a file and unstage changes
  • You understand amend vs revert vs reset
  • You know when to avoid reset on shared branches