The git clean command is used to remove untracked files and directories from a Git repository. These files are not tracked by Git and often appear during development as build artifacts, logs, or temporary files created by tools.
In this guide, you will learn how the git clean command works, how to safely remove untracked files using options such as -f, -fd, and -fx, and how to avoid accidentally deleting important files. If you specifically want to focus on deleting untracked files in different scenarios, see our guide on
Git Remove Untracked Files (Delete Untracked Files Safely).
Git Clean Quick Cheat Sheet
| Description | Command |
|---|---|
| Preview files that will 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 ignored and untracked files | git clean -fx |
| Remove files interactively | git clean -i |
| Preview directories that will be removed | git clean -nd |
| Remove specific directory recursively | git clean -fd <directory> |
| Clean repository including build artifacts | git clean -fdx |
What Does git clean Do in Git
The git clean command is used to remove untracked files from a Git repository's working directory. Untracked files are files that exist in the project directory but are not currently tracked by Git and have not been added to the staging area.
This command is commonly used to clean temporary files, build artifacts, or leftover files generated during development. Unlike commands such as git reset or
git restore, git clean specifically targets files that Git does not track.
Difference between tracked and untracked files
Tracked files are files that Git already manages. These files have either been committed previously or are currently staged for commit.
Untracked files, on the other hand, are files that exist in the working directory but have never been added using the git add command.
You can identify untracked files using git status:
git statusGit will list them under the Untracked files section.
When you should use git clean
You should use git clean when you want to remove unnecessary files that Git is not tracking.
Common situations include:
- Removing build artifacts after compilation
- Cleaning temporary files created during development
- Resetting a repository to a clean working state before running builds or tests
- Cleaning a repository before switching environments or branches
Remove All Untracked Files from Repository
Over time, development tools may create temporary files such as logs, cache files, or compiled artifacts. These files are often not tracked by Git and can clutter the repository.
Delete untracked files using git clean -f
To remove all untracked files from the working directory, run:
git clean -fExplanation:
-fstands for force, which is required because Git prevents accidental file deletion by default.
Before deleting files, you can preview the files that will be removed:
git clean -nThis performs a dry run and lists the files that would be deleted.
Delete untracked files and directories using git clean -fd
By default, git clean removes only untracked files. To remove both files and directories, use:
git clean -fdExplanation:
-fforces deletion-dincludes untracked directories
This is useful for removing directories such as build/, dist/, or tmp/ that were generated during development, often after running builds triggered by
git pull.
Remove Ignored Files in Git
Some files are intentionally excluded from version control using a .gitignore file. These files are typically build outputs, dependency folders, or configuration files.
Remove ignored files only
To delete files that are listed in .gitignore, run:
git clean -fXThe capital X flag tells Git to remove ignored files only, while keeping other untracked files intact.
Remove both ignored and untracked files
To remove both ignored files and other untracked files, use:
git clean -fxExplanation:
-fforces deletion-xremoves ignored and untracked files
This command is often used when you want to completely clean the repository, for example before running a fresh build.
Clean Repository Before Build or Deployment
In many development workflows, temporary files are generated during builds, tests, or dependency installation. Cleaning these files ensures the working directory remains consistent.
Clean build artifacts
Build tools often create folders such as:
build/dist/target/out/
To remove these generated files and start from a clean state, run:
git clean -fdThis removes all untracked files and directories.
Remove temporary files generated during development
Temporary files such as logs, cache files, or compiled binaries may accumulate during development.
A common workflow is:
git clean -n
git clean -fdThe first command previews the files that will be deleted, while the second command removes them.
This ensures your repository is clean before running builds, tests, or deployments.
Remove Untracked Files After git reset
When developers use git reset, they often expect the repository to return to a completely clean state. However, this command only affects tracked files and commit history. Any untracked files remain in the working directory unless they are removed manually.
Why untracked files remain after reset
The git reset command resets the commit history, staging area, or tracked files, depending on the option used. For example:
git reset --hard HEADThis restores tracked files to the latest commit but does not delete untracked files that exist in the working directory.
To see these leftover files, run:
git statusGit will list them under the Untracked files section.
Clean repository after reset
After running a reset, you can remove the remaining untracked files using git clean.
First preview the files that will be removed:
git clean -nThen delete them:
git clean -fdThis removes both untracked files and directories, leaving your repository in a clean state.
Undo git clean (Important)
One important thing to understand about git clean is that it permanently deletes files from the working directory.
Can git clean be reversed
Unlike git reset or
git restore, the git clean command cannot be undone. Once files are deleted, Git has no record of them because they were never tracked in the repository.
This is why Git requires the -f (force) option before executing the command.
Recover deleted files after git clean
If you accidentally remove important files using git clean, recovery options are limited. Possible ways to recover files include:
- Restoring from a backup
- Using IDE local history features
- Using file recovery tools on the operating system
Because recovery is difficult, it is always recommended to preview deletions first:
git clean -nGit Clean vs Git Reset vs Git Restore
Git provides multiple commands for discarding changes, but each command works differently.
When to use git clean
Use git clean when you want to remove untracked files from the working directory.
Example:
git clean -fTypical use cases include cleaning build artifacts or temporary development files.
When to use git reset
Use git reset when you want to modify the commit history or staging area.
Example:
git reset --hard HEADThis restores tracked files to the last commit but does not affect untracked files.
When to use git restore
Use git restore when you want to revert changes to tracked files in the working directory.
Example:
git restore file.txtThis command does not remove untracked files and only restores tracked files.
Common Mistakes When Using git clean
Although git clean is useful, using it incorrectly can lead to accidental data loss.
Running git clean without preview
Many developers run git clean -f without checking which files will be deleted.
A safer workflow is:
git clean -n
git clean -fThe first command previews the deletion list.
Accidentally deleting ignored files
Running the following command removes ignored files as well:
git clean -fxThis may delete important files listed in .gitignore, such as environment configuration files.
Removing important local files
If important files were never committed or tracked by Git, running git clean will delete them permanently.
Before running the command, always check using git status:
git statusThis helps ensure no important files are accidentally removed.
Frequently Asked Questions
1. What does git clean do?
The git clean command removes untracked files from the working directory. It is commonly used to delete temporary files, build artifacts, or other files that are not tracked by Git.2. How do you remove untracked files in Git?
You can remove untracked files using the commandgit clean -f. To remove both untracked files and directories, use git clean -fd.3. How can I preview files before deleting them with git clean?
You can preview which files will be removed by runninggit clean -n. This performs a dry run and lists all files that would be deleted.4. Can git clean remove ignored files?
Yes. Runninggit clean -fX removes ignored files only, while git clean -fx removes both ignored and untracked files.5. Can you undo git clean?
No. The git clean command permanently deletes untracked files. Once removed, they cannot be recovered unless you restore them from a backup or another copy.Summary
The git clean command helps remove untracked files and directories from a repository's working directory. It is commonly used to clean temporary files, build artifacts, and other files that Git does not track.
Before deleting files, always perform a dry run using git clean -n to preview which files will be removed. Using the command carefully ensures your repository remains clean without accidentally deleting important data. For full cleanup workflows, also see
git reset examples.




