How to Remove Untracked Files from the Current Working Tree in Git

  1. Steps to removing untracked files from working tree
    1. Checking the files and directories
    2. Deleting files
  2. Git Working Tree
  3. Tracked and Untracked Files
  4. Cleaning
  5. Git Ignore Files

Sometimes, you need to exclude the unnecessary files which are left by merges or created by mistake, to clean up your repository. You can either add these files in .gitignore or delete them.

Steps to removing untracked files from working tree

We will show you how to delete the untracked files from the current working tree step by step:

Checking the files and directories

Run the git clean with -d and -n options to see what files and directories will be removed:

git clean -d -n

The output of the command above will list the untracked files to be deleted after the cleaning.

If there are important files in the list above, you can track them with the git add command:

git add <file>

Deleting files

If you are sure about deleting the files, you can run the git clean command with the -f option:

git clean -f

For deleting the directories, also, you can add the -d option as follows:

git clean -fd

The git clean command has an interactive mode that is activated while passing the -i option. To delete the untracked files and folders interactively run:

git clean -d -i

The git clean command, also, has an option of deleting the ignored files and directories. To do that, you should add the -x option:

git clean -d -n -x

The command above will show all the files and folders to be deleted after running git clean -dfx:

To delete only the ignored files and directories use the -X option, instead. That deletes all files and directories listed in the .gitignore and keeps the not ignored untracked files.

Git Working Tree

In Git, the Working Tree is your file system directory (including files and subdirectories) associated with a git repository. It is filled with the files you adjust, where you insert new files and from which you delete the unnecessary files. Any time opening the files for a project managed by a repository, you access the Working Tree.

Tracked and Untracked Files

Each file in your working directory can be either in tracked or in untracked state. Tracked files are those that were in the last snapshot. Files in this state can be modified, unmodified, or staged and recognized by Git.

Untracked files are the files in your working directory that are not in the last snapshot and not in the staging area. When you first git clone a repository, all of your files will be tracked and unmodified.


The git clean is an undo command completing the git reset and git checkout commands. However, the git clean command runs on untracked files, unlike the other commands that work on the files that are added to the tracking index.

Git Ignore Files

In the working copy, the files are visible to Git as one of the following: tracked, untracked, and ignored. Ignored files are those that Git has been instructed to ignore. These files have to be derived from the repository source so as to be committed. They are kept in a .gitignore file. They are edited and committed manually because git ignore command doesn’t exist.

Do you find this helpful?

Related articles