Throw away uncommitted edits with git restore, the older git checkout -- form, or git clean for untracked files, and learn which commands cannot be undone.
The short answer
#Use git restore with the file name to put a tracked file back to how it was in the last commit. For new files Git is not tracking yet, use git clean, and always run it with -n first to see what it will delete.
_10git restore src/app.ts # discard edits to one file_10git restore . # discard edits to every tracked file_10git clean -n # preview untracked files to delete_10git clean -f # actually delete them
Why it happens
#Git tracks three versions of a file: the one in the last commit (HEAD), the one in the index (staged), and the one on disk. "Discarding changes" means copying an older version back over the copy on disk.
Which command you need depends on where the change is:
- Edited but not staged: restore the working tree from the index.
- Edited and staged: restore both the index and the working tree from
HEAD. - A brand new file that has never been added: Git has no older version, so the only option is to delete it, which is what
git cleandoes.
The important thing to know is that uncommitted work that you discard is gone. Git cannot bring it back because it was never stored in a commit.
How to fix it
#Discard unstaged edits with git restore
_10git status_10# Changes not staged for commit:_10# modified: src/app.ts_10_10git restore src/app.ts
This copies the staged version (which is the committed version if you have not staged anything) back over the file. You can pass several paths, a directory, or . for everything. Add -p to pick individual hunks to throw away and keep the rest.
Discard staged and unstaged edits together
If you already ran git add, plain git restore leaves the staged version in place. Restore both from the last commit:
_10git restore --staged --worktree src/app.ts
You can also restore a file from any other commit or branch with --source:
_10git restore --source=main src/app.ts # take the version on main_10git restore --source=HEAD~2 src/app.ts # take the version from two commits ago
The older git checkout form
Before Git 2.23, the usual command was git checkout with a double dash. It still works and you will see it in many older answers:
_10git checkout -- src/app.ts_10git checkout HEAD -- src/app.ts # also resets the staged copy
The -- tells Git that what follows is a path, not a branch name. Without it, a file and a branch with the same name are ambiguous.
Remove untracked files with git clean, carefully
git clean deletes files that Git does not track. It refuses to run without -f by default, which is a good thing.
_10git clean -n # dry run: list what would be deleted_10git clean -f # delete untracked files_10git clean -fd # also delete untracked directories_10git clean -i # interactive: choose what to delete
By default it leaves ignored files alone. Adding -x also deletes ignored files such as node_modules and .env, which is rarely what you want.
Keep a safety net with git stash
If you are not completely sure you will never want the changes, stash them instead of deleting them. You can drop the stash later.
_10git stash push -u -m "experiment I might want back"
Common pitfalls
#- Running
git clean -fdxin a project with a local.envor other ignored config. Those files are deleted too and are not in Git to recover. - Expecting
git restore fileto unstage. It changes the working tree; usegit restore --staged fileto unstage only. - Using
git reset --hardwhen you meant one file. It discards every uncommitted change in every tracked file. - Discarding work that was never committed or stashed. Git has no copy, so it cannot be undone.
- Forgetting the
--ingit checkout -- namewhen a branch has the same name as the file.
Related reading
#Was this resource helpful?