How to Remove Untracked Files in Git

Updated on

3 min read

Git Remove Untracked Files

The files in the Git working directory can be either tracked or untracked.

Tracked files are the ones that have been added and committed, and Git knows about. Tracked files can be unmodified, modified, or staged. All other files in the working directory are untracked and git is not aware of those files.

Sometimes your git working directory may get cluttered up with unnecessary files that are either auto-generated, leftover from merges, or created by mistake. In those situations, you can either add those files in .gitignore or remove them. If you want to keep your repository nice and clean, the better option is to remove the unnecessary files.

This article explains how to remove untracked files in Git.

Removing Untracked Files

The command that allows you to remove untracked files is git clean.

It is always a good idea to backup your repository because once deleted, the files and changes made to them cannot be recovered.

Before running the actual command and removing untracked files and directories use the -n option that will perform a “dry run” and show you what files and directories will be deleted:

git clean -d -n 

The output will look something like this:

Would remove content/test/
Would remove content/blog/post/

If some of the files listed above are important, you should either start tracking these files with git add <file> or add them to your .gitignore.

Once you are sure you want to go ahead and delete the untracked files and directories, type:

git clean -d -f

The command will print all successfully deleted files and directories:

Removing content/test/
Removing content/blog/post/

The -d option tells git to remove untracked directories too. If you don’t want to delete empty untracked directories, omit -d option.

The -f option stands for force. If not used and the Git configuration variable clean.requireForce is set to true, Git will not delete the files.

To interactively delete the untracked files, use the -i option:

git clean -d -i

The output will show the files and directories to be removed, and ask you what to do with those files:

Would remove the following items:
  content/test/   content/blog/post/
*** Commands ***
    1: clean                2: filter by pattern    3: select by numbers
    4: ask each             5: quit                 6: help

Select one of the choices and hit Enter.

If you want to limit the clean operation to given directories, pass the paths to the directories to be checked for untracked files as arguments to the command. For example, to check for files under the src directory, you would run:

git clean -d -n src

Removing Ignored Files

The git clean command also allows removing ignored files and directories.

To remove the all ignored and untracked files, use the -x option:

git clean -d -n -x

If you want to remove only the ignored files and directories, use the -X option:

git clean -d -n -X

The command above will delete all files and directories listed in your .gitignore and keep the untracked files.


In this tutorial, we have shown you how to delete untracked files and directories in Git. Remember to always dry run the command before actually deleting files.

If you have feedback, leave a comment below.