Removing Untracked Files With git clean
Delete untracked files safely with git clean: why -f is required, dry runs with -n, folders with -d, ignored files with -x and -X, excluding patterns, interactive mode, nested repositories, and combining clean with reset --hard to return to a pristine checkout.
kvi5hnuCode Sprout team member5 min readOn this page
git restore and git reset --hard undo changes to tracked files, but they leave untracked files alone: new files you never added, build outputs, logs, scratch folders. git clean deletes those. It's the one Git command that deletes files Git has never stored, so they can't be recovered from Git afterwards. Use it carefully: dry run first, every time.
Dry run, then force
git init -q trail-guide && cd trail-guide
printf 'build/\n*.log\n' > .gitignore
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
mkdir -p build scratch
echo "binary" > build/site.zip # ignored
echo "debug" > server.log # ignored
echo "draft" > draft.txt # untracked
echo "test" > scratch/try.txt # untracked folder
echo "SECRET=1" > local.env # untracked
git clean; echo "exit status $?"
echo "=== -n: dry run, files only"
git clean -n
echo "=== -nd: folders too"
git clean -nd
echo "=== remove them"
git clean -fd
git status --short --ignored
fatal: clean.requireForce is true and -f not given: refusing to clean
exit status 128
=== -n: dry run, files only
Would remove draft.txt
Would remove local.env
=== -nd: folders too
Would remove draft.txt
Would remove local.env
Would remove scratch/
=== remove them
Removing draft.txt
Removing local.env
Removing scratch/
!! build/
!! server.log
- By default,
clean.requireForceistrue, sogit cleandoes nothing without-f(--force), or-nor-i. That's deliberate, and worth keeping. -n(--dry-run) lists what would be deleted. Run it first, then repeat the command with-finstead of-n.-dincludes untracked folders. Without it,git cleanremoves only untracked files directly in folders Git already tracks.- Ignored files stay unless you ask for them, so
build/andserver.logsurvived.
Ignored files: -x and -X
| Option | Removes |
|---|---|
| (none) | untracked files that aren't ignored |
-x | untracked files including ignored ones |
-X | only ignored files, keeping your other untracked files |
git init -q trail-guide && cd trail-guide
printf 'build/\n*.log\n' > .gitignore
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
mkdir -p build; echo "binary" > build/site.zip; echo "debug" > server.log
echo "draft" > draft.txt
echo "=== -ndX: build outputs only"
git clean -ndX
echo "=== -ndx: everything untracked"
git clean -ndx
echo "=== -ndx, but keep drafts"
git clean -ndx -e 'draft*'
=== -ndX: build outputs only
Would remove build/
Would remove server.log
=== -ndx: everything untracked
Would remove build/
Would remove draft.txt
Would remove server.log
=== -ndx, but keep drafts
Would remove build/
Would remove server.log
-Xis the "clean build" option: it removes build outputs, caches and logs listed in.gitignore, and nothing you're working on.-xreturns the folder to exactly what's committed. It also deletes ignored files you may want, such as.envfiles with local settings, editor settings or downloaded dependencies.-e <pattern>(--exclude) protects extra patterns from this run, on top of the ignore rules.
Paths limit the clean to part of the tree: git clean -fd -- docs/.
Interactive mode
git clean -i lists the candidates and lets you filter them before anything is deleted:
git init -q trail-guide && cd trail-guide
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "draft" > draft.txt; echo "SECRET=1" > local.env; mkdir scratch; echo "t" > scratch/try.txt
# answers: 2 = filter by pattern, "*.env" to keep, empty line to finish, 1 = clean
printf '2\n*.env\n\n1\n' | git clean -id > /dev/null
ls
lake.txt
local.env
The menu offers filter by pattern (remove items from the list), select by numbers, ask each (a yes/no question per item) and clean. It's the safest way to clean when the list is mixed.
Nested repositories
A folder that contains its own .git, such as a separate project cloned inside yours, isn't removed by git clean -fd, because it could hold unpushed work. Git reports nothing about it. To delete it anyway, give -f twice: git clean -ffd. Submodules are handled separately (article 41).
A pristine checkout
To make the working tree exactly match the last commit, undoing every change to tracked files and deleting every untracked file, combine the two commands:
git reset --hard # tracked files back to HEAD
git clean -fdx # every untracked file, ignored ones too
Run git clean -ndx first and read the list. This is common in CI scripts and before building a release, but on your own machine prefer -X or a plain -fd, which keep local configuration.
To keep a file safe from -x without committing it, there's no special option. Keep such files outside the repository, or remember -e. git stash -u (article 27) is a reversible alternative: it sets untracked files aside instead of deleting them.
What's current
clean.requireForcedefaults totrue, and current Git says so plainly:clean.requireForce is true and -f not given: refusing to clean. Setting it tofalseis possible but removes the main safety net.- Interactive mode (
-i) makes mixed cleanups safe without a separate dry run. git cleandoesn't use the trash: deleted files are gone unless your editor, backups or operating system keep copies.
Common questions
I ran git clean -fdx and deleted my .env file. Can Git restore it?
No. Untracked and ignored files were never stored in Git. Check backups, your editor's local history, or the operating system's file history. Next time, run -n first, or use -X with an -e exclusion, or git stash -u.
Why does git clean -f leave folders behind?
It only deletes untracked files in folders Git already tracks. Add -d to delete untracked folders as well.
What's the difference between git clean and git rm?
git rm removes tracked files and stages the removal for the next commit. git clean deletes untracked files and has nothing to do with commits.
How can I see exactly which files are untracked before cleaning?
git status --short shows them with ??, and git status --ignored adds ignored ones (!!). git clean -nd and -ndx show what each clean would remove.
Summary
git cleandeletes untracked files, and they can't be recovered from Git.- It needs
-f,-nor-i. Always dry run with-nfirst. -dincludes folders,-xincludes ignored files, and-Xremoves only ignored files.-e <pattern>protects files for one run, and-ilets you filter interactively.- Nested repositories need
-ff. git reset --hard && git clean -fdxgives a pristine checkout, and also deletes local settings files.
Practice questions
Your build left files in dist/ and *.cache, both listed in .gitignore. You also have an untracked notes.md you want to keep. Which command removes only the build files?
Answer
git clean -ndX to check, then git clean -fdX. -X removes only ignored files, so dist/ and the cache files go and notes.md, untracked but not ignored, stays. -d is needed to remove the dist/ folder itself.
What's the difference between git clean -fd and git clean -fdx?
Answer
-fd removes untracked files and folders, but keeps ignored ones such as build outputs and .env files. -fdx removes ignored ones too, leaving only tracked files. -x is the dangerous one, because ignored files often include local configuration.
A colleague wants to "reset everything" with git reset --hard, but new files keep appearing in git status afterwards. Why, and what completes the job?
Answer
git reset --hard only resets tracked files. Untracked files are untouched. git clean -nd shows them, and git clean -fd removes them, adding -x only if ignored files should go too. If any might be needed, git stash -u sets them aside instead.
Review quiz
Test yourself on Part 5 with the Git Part 5 review quiz.
Previous: Stashing Work in Progress: push, pop, apply, Partial Stashes and Branches
Next: Naming Commits: Revision Syntax, Ancestry, Ranges and Reflog References

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.