Using Git Clean Command to Remove Untracked Files in Git

Featured image for the Git Clean Command Tutorial

The Git clean command can be used to remove the untracked files as follows:Forcefully deleting untracked files command:

$ git clean -f

For interactively deleting files, use the -i option:

$ git clean -i

The above command displays the files that will be removed and gives options to choose (see examples in the next section).

$ git clean –n

Only displays what will be removed by the clean command. It is useful if you are unsure about the files/directories that are untracked. Maybe, you want to double check before permanently deleting the files.

The next section takes you through step by step for removing the untracked files and directors, but before that let us look briefly at what actually untracked files are.

What are untracked files in Git?

A file is tracked if it is under Git (or any other VCS) or exists in the Git index. On the other hand, untracked files are not controlled by the version control system.

The untracked files may be created as a result of compiling the code file or some other reason.

For example, if your C++ project contains a few files that you added to Git work tree:

  • main.cpp
  • mygame.cpp
  • score.cpp

During a build, it may generate these files:



These are untracked files. Similarly, you may create any other file yourself (e.g. help.doc, 123.txt) for some reason that you do not want to control by Git and ultimately want to remove.

An example of removing the untracked file by clear command

In the first example, I will use –f option in the clear command for removing a file. As mentioned earlier, using the –f option immediately removes the untracked files:

To understand that, consider we have the following files in our local directory that acts as the Git repository:

Git remove untracked

Out of these, the footer.php, haeder.php and are added to the repository or being tracked. This can be confirmed by running the following command:

$ git ls-files

The result should be:

Git ls-files

You can see, the ls-files command displayed the files in the working tree/index.

Removing the untracked files

The tst1.txt and tst2.txt files are untracked and we want to delete them.

To immediately delete both files, run this command:

$ git clean -f

The result should be:

Removing tst1.txt

Removing tst2.txt

If you look at the folder again, it should not display the text files anymore.

Using the interactive delete option

The clear command with –i / –interactive option lets you view files before actually deleting them from the file system.

Suppose, we have tst3.txt and tst4.txt files in our repository folder. Running the following command:

$ git clean –i

The result:

Git remove file interactive

You can see that Git is asking “what now>” and with each option, you may see the colored letter to perform the action.

If you press c, it will remove the untracked file while q quits the operation.

How to remove untracked directories?

The simple clean command with -f or –i options enables deleting the files in the current directory. What if you have untracked files in sub-folders?

First, have a look at the clean command with –i option as we have two untracked text files in the current directory and a subfolder (test_dir) in our local repository.

Git remove directory

After running the clean command:

Git untracked files

You can see, the interactive option did not display the test_dir, so it will not be removed if you press “c”.

Removing the directory by –d option

For removing a directory, you may use the –d option in the clean command. For example:

$ git clean -d -i

This command displays the untracked files and directories to be removed (so giving you a chance to review).

Git untracked folder

Similarly, to remove untracked folders and files immediately, you may also use this command:

$ git clean -d –f

Using –n option in the Git clean command

The clean command with –n option only displays the files and with –d – n flags, it displays the directories to be removed.

No files or folders are actually removed by running these commands:

$ git clean –n

Displays the files to be removed.

$git clean –d –n

Displays the folders along with files in current directories.

Removing specific file/directory example

Rather than removing all files and directories that are untracked by a single clean command, you may choose which file or directory to remove.

This is shown in the above examples; by using the clean command with –i or –interactive option.

Suppose, we have the same files and a directory i.e. tst1.txt, tst2.txt, and a directory test_dir, that are untracked.

As we run this command:

$ git clean –d –i

It results in the following:

Git clean options

You can see six options that are self-descriptive.

If you enter option 1 or c, it will remove all untracked files.

Author - Atiq Zia

Atiq is the writer at, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!