W3docs

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

While working with Git, it is sometimes necessary to get rid of local (untracked) files from your current Working Tree. Learn how to do it in this snippet.

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 <kbd class="highlighted">-d</kbd> and <kbd class="highlighted">-n</kbd> options to see what files and directories will be removed:

file and directory will be removed git, git clean

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:

track important files, git add

git add &lt;file&gt;

Deleting files

If you are sure about deleting the files, you can run the git clean command with the <kbd class="highlighted">-f</kbd> option:

track important files, git add

git clean -f

For deleting the directories, also, you can add the <kbd class="highlighted">-d</kbd> option as follows:

delete directories and folders git

git clean -fd

The git clean command has an interactive mode that is activated while passing the <kbd class="highlighted">-i</kbd> option. To delete the untracked files and folders interactively run:

delete the untracked files interactively git, git clean

git clean -d -i

The <kbd class="highlighted">git clean</kbd> command, also, has an option of deleting the ignored files and directories. To do that, you should add the <kbd class="highlighted">-x</kbd> option:

removes the ignored files and directories git, git clean

git clean -d -n -x

The command above will show all the files and folders to be deleted after running <kbd class="highlighted">git clean -dfx</kbd>:

To delete only the ignored files and directories use the <kbd class="highlighted">-X</kbd> option, instead. That deletes all files and directories listed in the <kbd class="highlighted">.gitignore</kbd> 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.

Cleaning

The git clean is an undo command completing the git reset and <kbd class="highlighted">git checkout</kbd> commands. However, the <kbd class="highlighted">git clean</kbd> 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 <kbd class="highlighted">.gitignore</kbd> file. They are edited and committed manually because git ignore command doesn’t exist.