Sub topic 5
Removing untracked files safely with git clean
This chapter assumes you can run git status, git add, and git commit, and that you know the working tree and index are separate areas. git restore handles files Git already tracks. It does nothing for files Git has never seen. git clean handles those.
Why restore does not remove new files
An untracked file is one that exists in the working tree but is not in the index or in HEAD. Git has no earlier version to restore it to, so git restore . leaves it alone. Build output, scratch notes, and generated files all pile up this way.
git clean deletes untracked files. Git has no copy of them, so they cannot be recovered from the reflog. Treat every git clean as permanent.
The safe workflow: preview, then delete
Git refuses to run git clean unless you pass -f (force) or -n, unless the clean.requireForce setting says otherwise. This guards against accidents. Always start with the dry run.
git clean -n
git clean -fd-n (dry run) prints what would be removed and deletes nothing. Output looks like this:
Would remove debug.log
Would remove tmp/Read the list. If every entry is something you want gone, run the real command. -f forces the deletion, and -d includes untracked directories. Without -d, Git skips directories.
Common options
| Option | Effect |
|---|---|
| `-n` | Dry run. Lists files without deleting them. |
| `-f` | Force. Required to delete. |
| `-d` | Also remove untracked directories. |
| `-i` | Interactive mode. Lets you pick which files to delete. |
| `-x` | Also remove files ignored by `.gitignore`. |
| `-X` | Remove only ignored files. |
By default, git clean leaves ignored files alone. That is usually what you want, because ignored files often hold local configuration such as .env or editor settings.
Ignored files
Adding -x removes everything untracked, including ignored files. That can delete local secrets, caches that take a long time to rebuild, and your own settings. Preview with -n first.
git clean -nx
git clean -fxChoosing files interactively
If the dry run shows a mix of files you want to keep and discard, use interactive mode.
git clean -iGit shows a menu. Choose "clean" to delete everything listed, "filter by pattern" to exclude some names, or "select by numbers" to pick specific entries. You can quit at any point without deleting anything.
Limiting the scope
You can pass a path so that only part of the tree is cleaned.
git clean -nd -- build/
git clean -fd -- build/Only untracked files under build/ are listed and removed. Use this when the rest of the tree holds files you want to keep.
A complete cleanup of a messy working tree
Combine the commands from this and earlier chapters when you want to return to exactly what HEAD contains.
git restore .
git clean -nd
git clean -fdgit restore . discards edits to tracked files in the working tree. The two clean commands preview and then remove untracked files and directories. Afterward, git status should report a clean working tree.
Common mistakes
- Running
git clean -fwithout a dry run first. - Forgetting
-d, then wondering why untracked directories remain. - Using
-xand deleting a.envfile or other ignored local configuration. - Expecting to recover a cleaned file with
git reflog. The reflog records commits and branch positions, and an untracked file was never part of one.
Exercise
Your project has an untracked notes/ directory with files you want to keep, plus an untracked debug.log and a tmp/ directory you want to delete. Which commands remove only debug.log and tmp/ safely?
Show answer
Run git clean -n -d to preview. Then run git clean -i and use "select by numbers" to choose debug.log and tmp/. Alternatively, run git clean -f -- debug.log tmp/, which limits deletion to those two paths. Without a path or interactive selection, git clean -fd would also delete notes/.
Exercise
You run git clean -f and an untracked directory scripts/ is still there. Why, and what changes the result?
Show answer
Without -d, git clean skips untracked directories. Run git clean -nd to preview, then git clean -fd to remove them.