Menu
Coddy logo textTech

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.

git clean deletes untracked files (??) from the working tree; tracked files are never touched.

Syntax

CommandWhat it does
git clean -nDry run - list what would be removed
git clean -fDelete untracked files (required force flag)
git clean -fdAlso remove untracked directories
git clean -fxAlso remove ignored files
git clean -fdxRemove everything untracked, dirs and ignored

Safe workflow

Preview, then delete - so nothing is removed by surprise.

StepCommandResult
1git clean -ndSee every file and folder that would go
2git clean -fdDelete 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.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED