Discard Git working-tree changes, including new files

Reset tracked files and the index to HEAD, preview untracked-file cleanup, and understand ignored-file, submodule, and recovery limits.

Best practices
Illustration of Git branches and version history

To return tracked files and the staging area to HEAD, use git restore --source=HEAD --staged --worktree -- . from the repository root. Then preview and remove unwanted untracked files with git clean.

Plain git restore . restores from the index: it discards unstaged changes but preserves staged changes. It does not necessarily return the repository to the last commit.

Inspect first, then restore

These commands discard work. Commit, copy, or stash anything you may need before proceeding:

git status --short
git diff
git diff --cached

# Run from the repository root.
git restore --source=HEAD --staged --worktree -- .

# Preview ordinary untracked files and directories.
git clean -nd

# Run only after reviewing that preview.
git clean -fd

The restore resets the index and tracked working-tree content. clean removes remaining ordinary untracked paths. Ignored files are retained by default, and nested Git repositories have additional protection. Submodule working trees are separate repositories; do not assume this sequence cleans their contents.

Choose clean options deliberately

Option Effect
-n Show what would be removed without deleting it
-f Permit deletion under the normal force requirement
-d Include untracked directories
-x Include ignored files as well as ordinary untracked files
-i Choose removals interactively

To include ignored build output, dependencies, or local configuration, preview the same expanded scope first:

git clean -ndx
git clean -fdx

This can remove .env files and other local data. It still is not a universal “fresh clone” operation: Git configuration, protected nested repositories, and separate submodule state can remain.

Save work with stash instead

If you are unsure about discarding changes:

git stash push -u -m "Save work before cleanup"
git stash list

-u includes ordinary untracked files, but excludes ignored files. -a also includes ignored files and can sweep in secrets or large build directories, so use it only with a reviewed scope. A stash is local to this repository and is not an external backup.

FAQ

Can Git recover files deleted by clean?

Usually not: files that were never added or committed have no recoverable Git version. A system backup or editor history may help, but the reflog does not record their contents. Preview before removal.

How does reset --hard differ?

git reset --hard HEAD resets tracked files and the index. It can delete untracked paths that obstruct writing tracked files, but it does not generally remove all untracked files. A separate, reviewed clean handles the remaining paths.

How do I remove an ignored .env file?

Include ignored files with -x, preferably restricting the operation to the exact intended path and previewing it first. If you want to retain local configuration, do not use a repository-wide clean -fdx.

See git-restore, git-clean, git-reset, and git-stash.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting