Git
Version Control
Untracked Files
Git Cleanup
Coding Tips

How to remove untracked files in Git?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Untracked files are files in your working tree that Git sees but is not currently tracking. They are often harmless build outputs, generated artifacts, scratch notes, or temporary files, but they can clutter git status and cause confusion if you are trying to inspect a clean working directory.

Start by seeing what would be removed

The main command for deleting untracked files is git clean, but it is intentionally conservative because it can permanently delete data. The safest first step is always a dry run.

bash
git clean -n

The -n flag means "show me what would happen, but do not actually delete anything." In most cases, this should be your first command.

If you also want to preview untracked directories, add -d.

bash
git clean -nd

Remove untracked files

Once the dry run output looks correct, remove untracked files with -f.

bash
git clean -f

Git requires -f because deleting untracked files is destructive. Without it, the command usually refuses to run.

To remove untracked directories too, use:

bash
git clean -fd

That is the common cleanup command in repositories that generate temporary directories during builds.

Ignored files versus untracked files

Ignored files are not the same as untracked files. An ignored file matches rules in .gitignore, .git/info/exclude, or a global ignore file.

If you want to remove only ignored files, use -X.

bash
git clean -fdX

If you want to remove both ignored and untracked files, use lowercase -x.

bash
git clean -fdx

That last form is powerful and risky. It can wipe build artifacts, local environment files, and anything else Git is not tracking.

Interactive cleanup

If you want more control, Git also supports interactive mode.

bash
git clean -i

Interactive mode lets you review candidates, filter them, and confirm deletions in steps. It is slower than a direct command, but it is safer when you are unsure.

Typical workflow

A good cleanup workflow looks like this:

  1. inspect the status
  2. preview cleanup with git clean -nd
  3. run the destructive form only after checking the list
bash
git status --short
git clean -nd
git clean -fd

That sequence reduces the chance of deleting a file you actually meant to keep.

When not to use git clean

Do not use git clean as a reflex whenever your working tree looks messy. If a file is important but untracked, git clean will not warn you that it contains valuable work.

It is also the wrong tool for deleting tracked changes. For tracked files modified in the working tree, you need a different Git command, and that distinction matters because tracked and untracked state are handled differently.

Common Pitfalls

The biggest mistake is running git clean -fdx without a dry run. That command is useful, but it is also the fastest way to delete local files you forgot were untracked.

Another common issue is assuming git clean affects tracked files. It does not. If the file is already tracked by Git, git clean will leave it alone.

A third problem is forgetting about ignored files. Developers often expect git clean -fd to remove everything generated by the build, but ignored files remain unless you use -X or -x.

Summary

  • Use git clean to remove untracked files from the working tree.
  • Start with git clean -n or git clean -nd to preview the deletion set.
  • Use git clean -f for files and git clean -fd for files plus directories.
  • Use -X for ignored files only and -x for ignored plus untracked files.
  • Treat git clean as destructive and verify the preview before deleting anything.

Course illustration
Course illustration

All Rights Reserved.