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
| Description | Command |
|---|---|
| List untracked files that would be removed (dry run) | git clean -n |
| Remove all untracked files | git clean -f |
| Remove untracked files and directories | git clean -fd |
| Remove ignored files only | git clean -fX |
| Remove both ignored and untracked files | git clean -fx |
| Remove files interactively | git clean -i |
| Remove a single untracked file manually | rm <file> |
| Remove an untracked directory manually | rm -rf <directory> |
| Check untracked files in repository | git status |
| Preview directories that will be deleted | git 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:
git clean -fThe -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:
git clean -nThis 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:
git clean -fdExplanation:
-fforces deletion-dremoves 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:
rm filenameExample:
rm debug.logThis deletes only the selected file without affecting other untracked files.
Delete selected files interactively
Git provides an interactive cleaning mode to select files manually.
git clean -iThis 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:
git clean -fXThe 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:
git clean -fxExplanation:
-fforces deletion-xremoves 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:
git reset --hard HEADThis 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:
git clean -fdA safer workflow is:
git clean -ndto preview the files first, and then run:
git clean -fdThis 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:
git clean -nIf the output looks correct, remove the untracked files:
git clean -fThis 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:
git clean -fdThis 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:
git clean -fTypical 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:
git reset --hard HEADThis 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:
git restore file.txtUnlike 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:
git clean -nThis helps confirm which files will be deleted.
Removing ignored files unintentionally
Using the command below removes both ignored and untracked files:
git clean -fxThis 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:
git clean -n
git clean -fPreview 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.




