Undoing and ignoring
Which tool to reach for depends on how far the change already went. Telling restore, reset, and revert apart is the whole point of this step.
What you'll learn
- You can safely throw away changes you haven't committed
- You can safely undo a that others already
- You keep and secrets out of the with
Which tool you need depends on how far the change already travelled. Not committed yet: git restore. Committed but not : git reset. Already pushed and pulled by others: git revert. Mix these up and you break other people's , so make it a habit to check where you stand with git status first.
git reset comes in three flavours. --soft undoes the commit but leaves the changes ; the default --mixed also unstages them; --hard throws the file contents away as well. --hard really does delete your work, so read the command once more before you press enter. Even then, don't give up immediately — git reflog often still holds the commit you lost.
Build output, node_modules, and secret files like .env must never enter the repository. Put a .gitignore at the and stops looking at those paths entirely. Careful though: a file already committed stays no matter what you add to .gitignore. Then you must untrack it with git rm --cached and commit — and if a password already went up, rotate the secret itself, because it lives on in history.
Understand it easily
Three tools for three situations. restore is an eraser for changes you have not committed yet. reset turns back the clock on your own commits that nobody else has. revert writes a new page that cancels an old one, which is the safe way once others have the history.
Commands for this step
Don't just read them — type them into a real . Your hands have to remember, not your eyes.
git restore <file>- Throw away the unstaged changes in a file: it goes back to the staged , or to the last commit if nothing is staged. Lost edits cannot be recovered.
git reset --soft HEAD~1- Undo the last commit but keep its changes staged — the usual way to reword a message.
git revert <commit>- Create a new commit that undoes that one. History stays intact, so it's safe on shared .
git diff --staged- Review what you staged before committing. Make it a habit right before every commit.
More options — open when you need them
git restore <file> does not care who made an edit — your own unstaged work in that file is lost too. It also leaves staged changes and untracked (new) files alone. Use the commands below for those cases.
git restore --staged <file>- Unstage a file: it leaves the staging area, but your edits stay in the .
git restore -p <file>- Go through the changes in a file piece by piece and throw away only the parts you choose.
git clean -n- List the untracked files that
git clean -fwould delete.git restorenever touches them, so check this list before you delete anything.
Common mistake and how to fix it
Using git reset --hard to undo and losing work that was never committed. That work cannot be brought back. Before any undo, run git status, and keep a copy of unfinished work with git stash.
You mark this yourself. Nothing is graded here.
Mark this step as understood?
This cannot be undone yet, so mark it only when you really understand.
Marked as understood
You mark this yourself. Nothing is graded here.