More Git answers

by Danny
Published 2 min read

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.


_10
git restore src/app.ts # discard edits to one file
_10
git restore . # discard edits to every tracked file
_10
git clean -n # preview untracked files to delete
_10
git 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 clean does.

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


_10
git status
_10
# Changes not staged for commit:
_10
# modified: src/app.ts
_10
_10
git 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:


_10
git restore --staged --worktree src/app.ts

You can also restore a file from any other commit or branch with --source:


_10
git restore --source=main src/app.ts # take the version on main
_10
git 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:


_10
git checkout -- src/app.ts
_10
git 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.


_10
git clean -n # dry run: list what would be deleted
_10
git clean -f # delete untracked files
_10
git clean -fd # also delete untracked directories
_10
git 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.


_10
git stash push -u -m "experiment I might want back"

Common pitfalls

#
  • Running git clean -fdx in a project with a local .env or other ignored config. Those files are deleted too and are not in Git to recover.
  • Expecting git restore file to unstage. It changes the working tree; use git restore --staged file to unstage only.
  • Using git reset --hard when 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 -- in git checkout -- name when a branch has the same name as the file.
#

Was this resource helpful?