Git Clean

Last Updated : 20 Jan, 2026

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 -n

This 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 -f

3. Remove Untracked Directories

To remove both untracked files and directories:

git clean -fd

4. Interactively Remove Files

For controlled cleanup, use interactive mode:

git clean -i

5. Remove Ignored Files

  • To remove only ignored files:
git clean -fX
  • To remove both ignored and untracked files:
git clean -fx

Git 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"

git clean
Git Clean

Example 2: Using "git clean -n"

git clean -n
Git Clean

Example 3: Using "git clean --force"

git clean --force
Git Clean

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

git status
Git Clean

Example 5: Using "git clean -fdx"

gir clean -fdx
Git Clean

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 -n

2. Force Clean (-f / --force)

Git does not remove files unless explicitly forced.

git clean -f

3. Remove Ignored Files (-X or -x)

  • Remove only ignored files:
git clean -fX
  • Remove both ignored and untracked files:
git clean -fx

4. Interactive Clean (-i / --interactive)

Allows selective deletion with user confirmation.

git clean -i

5. Remove Untracked Directories (-d)

By default, directories are not removed.

git clean -fd

Git 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 clean

Using 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 -fd

Interactive 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 --interactive

Git 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.
Comment
Article Tags:

Explore