git clean removes untracked files and directories from the working directory to keep the repository clean.
- Removes untracked files from the working directory.
- Does not affect tracked or staged files.
- -d removes untracked directories.
- -n performs a dry run (shows what would be deleted).
- -f is required to force deletion.
- -x removes ignored and untracked files.
- -fx force-removes both ignored and untracked files.
- Commonly used before branch switching or fresh builds.
Note: git clean is destructive and irreversible, so always use -n first to preview changes.
Using git clean Safely
Before running git clean, evaluate its impact carefully, as it permanently deletes files and should be used with caution.
1. Preview the Changes
Use the dry-run option to see what will be deleted without removing anything:
git clean -nThis lists all untracked files and directories that would be removed.
2. Remove Untracked Files
After reviewing the output, force deletion of untracked files:
git clean -f3. Remove Untracked Directories
To remove both untracked files and directories:
git clean -fd4. Interactively Remove Files
For controlled cleanup, use interactive mode:
git clean -i5. Remove Ignored Files
- To remove only ignored files:
git clean -fX- To remove both ignored and untracked files:
git clean -fxGit Clean Examples
There are certain limitations associated which are as follows:
By default, it will not remove-
- the .gitignore files
- new directories which are created recently
- index files.
- existing commit files
Let us pictorially depict the above commands and interpret them via terminal/PowerShell as follows:
Example 1: Using "git clean"

Example 2: Using "git clean -n"

Example 3: Using "git clean --force"

Example 4: Using "git status" (To check the status of files)

Example 5: Using "git clean -fdx"

Git Clean Common Options
git clean provides multiple options to control how untracked and ignored files are removed, allowing safe previews, selective cleanup, and force deletion as needed.
1. Dry Run (-n / --dry-run)
Preview what will be deleted without actually removing anything.
git clean -n2. Force Clean (-f / --force)
Git does not remove files unless explicitly forced.
git clean -f3. Remove Ignored Files (-X or -x)
- Remove only ignored files:
git clean -fX- Remove both ignored and untracked files:
git clean -fx4. Interactive Clean (-i / --interactive)
Allows selective deletion with user confirmation.
git clean -i5. Remove Untracked Directories (-d)
By default, directories are not removed.
git clean -fdGit Cleanup Best Practices
Regular cleanup helps keep your Git repository synchronized, organized, and efficient.
Prune Remote Branches
Removes local references to deleted remote branches, keeping the repository in sync.
git remote prune <remote-name>Remove Merged Branches
Deletes local branches that have already been merged into the main branch.
git branch -d <branch-name>Delete Local and Remote Tags
Removes unnecessary tags after merges or releases.
git tag -d <tag_name>
git push --delete origin <tag_name>Clean Untracked Files
Deletes untracked files and directories from the working directory (use with caution).
git cleanUsing git clean -fd Safely
git clean -fd is used to forcibly remove untracked directories along with untracked files from the working directory.
- -f forces deletion.
- -d removes untracked directories.
- Permanently deletes files and folders.
- Always run a dry run before execution.
git clean -fdInteractive Mode Or Git Clean Interactive
Interactive mode allows you to clean untracked files and directories with confirmation, reducing the risk of accidental deletion.
- Default git clean runs without user prompts.
- --interactive asks for confirmation before deletion.
- Displays files and directories to be removed.
- Safer approach for controlled cleanup.
git clean --interactiveGit Clean Command Limitations
git clean permanently deletes untracked files, but it has specific technical limitations that must be considered.
- Permanent Deletion: Untracked files and directories are deleted permanently; always take a backup first.
- Limited Scope: Does not remove tracked files; ignored files require additional options.
- No Confirmation: Runs without prompts unless --interactive is used.
- Directories Not Removed by Default: Use -d to delete untracked directories.