Git Remove Untracked Files (Delete Untracked Files Safely)

Git Remove Untracked Files (Delete Untracked Files Safely)

Untracked files in Git are files present in your working directory that Git is not currently tracking. These files are often created during development as logs, temporary files, build artifacts, or configuration files that were never added to version control.

In this guide, you will learn how to remove, delete, or discard untracked files in Git safely using different methods such as the git clean command and manual file removal. If you want to understand how the command itself works in detail, see our guide on Git Clean Command (Remove Untracked Files Safely).


Remove Untracked Files in Git Cheat Sheet

DescriptionCommand
List untracked files that would be removed (dry run)git clean -n
Remove all untracked filesgit clean -f
Remove untracked files and directoriesgit clean -fd
Remove ignored files onlygit clean -fX
Remove both ignored and untracked filesgit clean -fx
Remove files interactivelygit clean -i
Remove a single untracked file manuallyrm <file>
Remove an untracked directory manuallyrm -rf <directory>
Check untracked files in repositorygit status
Preview directories that will be deletedgit clean -nd

Remove All Untracked Files from Repository

When working on a project, temporary build files, logs, or test artifacts may accumulate in the repository. These files are not tracked by Git and can clutter your working directory. The git clean command helps remove them safely.

Delete all untracked files

To remove all untracked files from the repository, run:

bash
git clean -f

The -f flag forces Git to delete untracked files. Git requires this flag to prevent accidental deletion.

You can preview which files will be deleted before running the command:

bash
git clean -n

This performs a dry run and lists all files that would be removed.

Delete untracked directories

By default, git clean removes only files. To remove untracked directories as well, use:

bash
git clean -fd

Explanation:

  • -f forces deletion
  • -d removes untracked directories

This is useful when directories like build/, dist/, or temporary folders are generated during development.


Remove Specific Untracked Files

Sometimes you may want to remove only certain files instead of clearing everything from the working directory.

Delete a single untracked file

You can remove a specific untracked file using the standard shell command:

bash
rm filename

Example:

bash
rm debug.log

This deletes only the selected file without affecting other untracked files.

Delete selected files interactively

Git provides an interactive cleaning mode to select files manually.

bash
git clean -i

This opens an interactive menu where you can:

  • list files to remove
  • filter files
  • select files individually
  • confirm deletion

Interactive cleaning is helpful when you want more control before deleting files.


Remove Ignored Files

Some files are intentionally excluded from Git tracking using the .gitignore file. These files are usually build artifacts, environment files, or dependency directories.

Delete ignored files only

To remove files that are listed in .gitignore, use:

bash
git clean -fX

The capital X tells Git to delete ignored files only.

Delete ignored and unignored files

To remove both ignored files and other untracked files together, use:

bash
git clean -fx

Explanation:

  • -f forces deletion
  • -x removes ignored and untracked files

This command is useful when you want a completely clean working directory, especially before running builds or tests.


Remove Untracked Files after git reset

Developers often assume that running git reset removes unwanted files, but this is not always true.

Why untracked files remain after reset

The git reset command modifies the commit history and staging area, but it does not delete untracked files from the working directory.

For example:

bash
git reset --hard HEAD

This resets tracked files to the latest commit but leaves untracked files untouched.

Clean repository after reset

After resetting the repository, you can remove leftover untracked files using:

bash
git clean -fd

A safer workflow is:

bash
git clean -nd

to preview the files first, and then run:

bash
git clean -fd

This ensures your working directory is completely clean and matches the repository state.


Remove Untracked Files Before Commit or Build

In many development workflows, temporary files are created during builds, testing, or dependency installation. Cleaning these files ensures the repository remains tidy and prevents unnecessary files from affecting builds or commits.

Clean working directory before build

Before running a build process, you may want to ensure the working directory contains only tracked files. This avoids conflicts caused by leftover temporary files.

First preview what will be removed:

bash
git clean -n

If the output looks correct, remove the untracked files:

bash
git clean -f

This ensures that your repository starts from a clean working state before compilation or packaging.

Clean temporary files generated during development

Development tools often generate temporary folders such as build, dist, target, or log files.

To remove both untracked files and directories created during development, run:

bash
git clean -fd

This is commonly used in CI/CD pipelines or when preparing a repository for deployment.


Git Clean vs Git Reset vs Git Restore

Several Git commands can discard changes, but they affect different parts of the repository. Understanding their differences helps prevent accidental data loss.

When to use git clean

Use git clean when you want to remove files that Git is not tracking.

Example:

bash
git clean -f

Typical use cases include:

  • Removing temporary build files
  • Cleaning generated directories
  • Preparing a repository for a fresh build

When to use git reset

The git reset command changes the commit history or staging area, but it does not delete untracked files.

Example:

bash
git reset --hard HEAD

This resets tracked files to the last commit, but untracked files will still remain in the working directory.

When to use git restore

The git restore command restores files to their previous state. It is mainly used for tracked files.

Example:

bash
git restore file.txt

Unlike git clean, this command does not affect untracked files.


Common Mistakes When Removing Untracked Files

Accidentally deleting important files

Running git clean -f deletes files permanently from the working directory. If important files are untracked, they will be removed.

Always run a preview first:

bash
git clean -n

This helps confirm which files will be deleted.

Removing ignored files unintentionally

Using the command below removes both ignored and untracked files:

bash
git clean -fx

This may delete important files such as environment configuration files or dependency folders listed in .gitignore.

Use this option carefully.

Running git clean without preview

One of the most common mistakes is executing git clean -f without checking the affected files.

Best practice workflow:

bash
git clean -n
git clean -f

Preview first, then delete.


Summary

Untracked files are files present in your working directory that Git does not track. Over time, these files can accumulate and clutter the repository.

The git clean command provides a safe and efficient way to remove these files. Using options such as -f, -d, -X, and -x, you can selectively remove files, directories, or ignored files depending on your needs.

Before deleting files, always run a dry run using git clean -n to verify which files will be removed. This simple step prevents accidental data loss and keeps your repository clean and organized.


Official Documentation

Steve Alila

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.