Skip to content

How to Remove Untracked Files in Git (git clean)

DodaTech 2 min read

In this tutorial, you'll learn about How to Remove Untracked Files in Git (git clean). We cover key concepts, practical examples, and best practices.

The Problem

Your working directory is cluttered with untracked files from build artifacts, temporary logs, or editor backup files. Running git status shows a long list of untracked items that you want to remove without deleting each one manually.

Quick Fix

Step 1: Do a dry run first

See what git clean would delete without actually removing anything:

git clean -n
Would remove build.log
Would remove temp/
Would remove .cache/

Step 2: Remove untracked files

Delete untracked files in the current directory:

git clean -f
Removing build.log
Removing temp/
Removing .cache/

This removes untracked files but keeps directories.

Step 3: Remove untracked files and directories

Add -d to include directories:

git clean -fd
Removing build.log
Removing temp/
Removing .cache/
Removing dist/

Step 4: Interactive mode for selective removal

Use interactive mode to choose what to delete:

git clean -i
Would remove the following items:
  build.log  temp/  .cache/
*** Commands ***
  1: clean   2: filter by pattern   3: select by numbers   4: ask each   q: quit
What now>

Type 1 to clean or a to ask for each item.

Step 5: Remove ignored files too

By default, git clean does not remove files matched in .gitignore. To remove those as well:

git clean -fx

Use -x to include ignored files and -X to remove only ignored files.

Alternative Solutions

Use .gitignore to prevent files from appearing as untracked

Add common build artifacts and temp files to .gitignore:

# .gitignore
node_modules/
dist/
*.log
.cache/
.env

Use git stash for files you might need later

If you are not sure about deleting, stash the untracked files first:

git stash --include-untracked

Common Mistakes to Avoid

Running git clean -f without a dry run first. Deleted files cannot be recovered from Git. Always use -n (dry run) before -f.

Forgetting -d to remove directories. git clean -f skips directories. Use -fd to remove both files and directories.

Removing files that are in .gitignore with -x. The -x flag removes ignored files too, which may delete build outputs or configuration files you need.

Pro Tips

Use git clean -n before every cleanup. Make the dry-run a habit to see what would be deleted before committing to the removal.

Create a .gitignore template for your project type. Use GitHub's .gitignore templates for common project types to prevent build artifacts from appearing as untracked.

Use git clean -X to remove only ignored files. This is useful when you want to clean generated files but keep untracked source files.

Prevention

  • Add build artifacts and temp files to .gitignore so they never appear as untracked.
  • Run git clean -n frequently to stay aware of untracked files.
  • Use git status --short to spot unexpected untracked files before they accumulate.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro