Undoing Mistakes

View saved

Discard unstaged edits in a file

git restore can throw away uncommitted working-tree changes for a file. Only do this when you are sure you do not need those edits.

git restore notes.txt

Unstage a file

If you staged too much, unstage without deleting your edits.

git restore --staged notes.txt
git status

Rewrite the latest local commit carefully

git reset --soft HEAD~1 moves the branch back one commit but keeps the changes staged. Use this only for commits that have not been pushed, or when your team agrees it is safe.

git reset --soft HEAD~1

Prefer safer options on shared branches

  • Create a new commit that fixes the problem instead of rewriting published history
  • Avoid git reset --hard until you understand it discards uncommitted work
  • Never force-push main on a team project without explicit agreement
  • Use git status before and after every recovery command

Comments

One comment per signed-in account. Comments are saved with this page’s URL.