STEP 11 OF 13
×Undoing Changes Safely
Mistakes happen constantly. Git gives you several tools to undo them safely, depending on exactly what needs fixing.
Undoing unstaged changes
If you've edited a file but haven't staged it yet, you can discard those edits and restore the file to its last committed state.
Unstaging a file
If you staged a file by mistake, you can remove it from the staging area without losing your actual edits in the file.
Undoing a commit
Reset can move your branch back to an earlier commit, optionally keeping or discarding the changes from the commits you're undoing. Revert creates a brand new commit that undoes a previous one, which is safer for shared history.
>_ COMMANDS YOU'LL USE
git restore filename.txtDiscard uncommitted changes in a specific filegit restore --staged filename.txtUnstage a file without losing its editsgit reset --soft HEAD~1Undo the last commit but keep its changes stagedgit revert HEADCreate a new commit that undoes the previous one💡Prefer revert over reset on branches other people are also using — it doesn't rewrite shared history.
⚠️Common mistake: Using git reset --hard on a shared branch