Unstage a file or discard my edits
When to use this: You staged something by mistake, or you want to throw away edits and return a file to its last committed state.
Before you start
Steps
-
Unstage (safe: keeps your edits)
terminalgit restore --staged src/pages/index.astroMoves the file back from 'staged' to 'modified'. Nothing is lost.
-
Discard edits to one file (DESTRUCTIVE)
terminalgit restore src/pages/index.astroResets the file to the last commit. Your edits in it are gone for good.
-
Discard everything uncommitted (DESTRUCTIVE)
terminalgit restore .Reverts all tracked files. New untracked files stay.
-
Preview removal of untracked files
terminalgit clean -nd-nis a dry run: lists new files and folders Git would delete. Nothing is removed. -
Delete untracked files (DESTRUCTIVE)
terminalgit clean -fdActually deletes the files listed by the dry run. Add
-xonly if you also want ignored files removed (it will deletenode_modulesand.env).
How to verify it worked
git statusshows a clean tree, or only what you meant to keep.
How to undo it
Unstaging: stage again with git add <file>. Discarded edits: not recoverable by Git. Check your editor's local history or the Recycle Bin.
Common errors and fixes
error: pathspec '...' did not match any file(s) Check the path and spelling. Run git status to copy it. Q&A
How do I keep my edits but get them out of the way?
Stash them: git stash push -u. They can come back with git stash pop.
Why always run git clean -nd first?
clean removes files Git cannot restore. The dry run shows the list so you do not delete something important.