HOWTO · Git

How to Remove Untracked Files in Git

Preview untracked files with git clean, then remove files or directories safely. Learn how -x and -X handle ignored files.

On this page

To remove untracked files in Git, preview candidates with git clean -n, then run git clean -f after checking the list. Add -d when you also want to remove untracked directories. git clean deletes files from your working tree; Git cannot restore untracked files that were not committed or staged.

Preview Untracked Files Before Cleaning

An untracked file exists in your working tree but has not been added to Git’s index, so it is not included in commits. Run git status to see untracked paths; Git may summarize files inside an untracked directory as that directory. If you want to keep a file and start tracking it, use git add <file> instead of cleaning it.

Preview untracked files before deleting anything with git clean -n. The -n option performs a dry run: Git lists the files it would remove without deleting them. To include untracked directories in the preview, use git clean -n -d. Review each listed path; ignored files are not included by default.

Remove Untracked Files and Directories

After reviewing the preview, use -f to allow deletion. This self-contained example creates a temporary Git repository, previews one untracked file, and removes it. It leaves your current repository untouched. The observed output shows notes.txt first as a removal candidate, then confirms its removal.

scratch=$(mktemp -d)
trap 'rm -rf "$scratch"' EXIT
git -C "$scratch" init -q
printf 'temporary file\n' > "$scratch/notes.txt"
LC_ALL=C git -C "$scratch" clean -n
LC_ALL=C git -C "$scratch" clean -f
Would remove notes.txt
Removing notes.txt

For your working tree, use git clean -f for untracked files and git clean -f -d for untracked files and directories. First run the matching preview, git clean -n or git clean -n -d, and check its candidates. Git cannot undo deletion of files that were never committed or staged.

If you only want selected files gone, remove those paths with your file manager or a command such as rm <file>. To select candidates interactively, use git clean -i; Git presents a menu instead of removing every candidate at once. A GUI client can also show untracked files, but its labels and delete controls vary by application.

Handle Ignored Files and Other git clean Options

Git normally protects files matched by .gitignore from git clean. Ignored files can include build output, local configuration, or data you still need, so inspect their scope before including them.

  • -x includes ignored files along with other untracked files. Preview that broader cleanup with git clean -n -d -x, then use git clean -f -d -x only if you intend to remove those candidates.
  • -X selects ignored files only. Preview with git clean -n -d -X, then use git clean -f -d -X to remove only ignored files and directories.
  • -d includes untracked directories; -f permits deletion; -n previews; and -i opens interactive selection.

Choose -x when you want a broad cleanup that includes ignored and other untracked items. Choose -X when cleanup should target ignored items only. Check the preview before either command, especially if ignored paths contain local files you want to keep.