Git: Remove Untracked Files
Last updated
Untracked files are files Git isn't managing yet - build output, logs, scratch files. git clean deletes them from the working tree. Because it permanently removes files that were never committed, always preview first with -n (a dry run) before running it for real with -f.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Syntax
| Command | What it does |
|---|---|
git clean -n | Dry run - list what would be removed |
git clean -f | Delete untracked files (required force flag) |
git clean -fd | Also remove untracked directories |
git clean -fx | Also remove ignored files |
git clean -fdx | Remove everything untracked, dirs and ignored |
Safe workflow
Preview, then delete - so nothing is removed by surprise.
| Step | Command | Result |
|---|---|---|
| 1 | git clean -nd | See every file and folder that would go |
| 2 | git clean -fd | Delete them once you've confirmed the list |
Git remove untracked files FAQ
How do I remove untracked files in Git?
Use
git clean. First preview with git clean -n to list what would be deleted, then run git clean -f to actually remove untracked files. The -f (force) flag is required because clean is destructive by design.How do I remove untracked directories too?
Add the
-d flag: git clean -fd removes untracked files and untracked directories. Plain git clean -f only removes files, leaving empty untracked folders behind.Does git clean delete ignored files?
Not by default - files matched by
.gitignore are left alone. Add -x to include them (git clean -fx), or -fdx to wipe everything untracked including ignored files and directories. Use -x carefully, since it can delete local config and build caches you meant to keep.Can I undo git clean?
No.
git clean permanently deletes files that were never committed, so there's nothing in Git's history to recover them from. That's exactly why you should always run git clean -n first to review the list before deleting.Can I practice this online?
Yes. Open the terminal playground to run
git clean in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers managing your working tree step by step.