Removing Untracked Files Using Git Clean Command

“In simple words, the git clean is the “undo” option when using git. There are different options to undo in git, such as revert and reset. However, the git clean focuses on removing the untracked files. The untracked files are those yet to be added to the version control, and in most instances, they are the files generated when the code compiles.

The git clean is an excellent command to add to your arsenal of git commands, and with it, you will quickly and effortlessly remove untracked files. Let’s see how to use the git clean.”

When and Where to Use Git Clean

You may sometimes find yourself at a dead end and wish to start over from the initial clean working space. In such a case, the best option is to use the git clean as unlike other options like the git reset, which only discards tracked files, git clean will dump even the untracked files.

Git clean works fine with untracked files, but it has its limitations. Not all files can be removed using the command. For instance, you can’t remove:

  • .gitignore files
  • Recently created directories
  • Files linked to an existing commit
  • Historically tracked files
  • Files already added to the index

Besides, when using the git clean command, you have to use the -force option. Otherwise, you will get an error message, and the command won’t work.

That said, it’s time to see how to use the git clean command.

Git Clean Usage Options

To better understand how to use git clean, we will create an example of a git working space and have tracked and untracked files and directories.

To see the untracked files in your workspace, run the git status command. We can see that we have the example_of_untracked_dir and example_untrackedfile as our untracked file and directory.

1. Dry Run

Untracked files, once deleted, can’t be restored. As such, you need to be sure of the files to be removed. You can do so using the -n flag.

The command lets you see what files will get removed before initiating the cleaning process. Only proceed if you are satisfied with what will be deleted. Note that only files can get removed. The untracked folders are not listed. We will see how to go about that soon enough.

2. Deleting Untracked Files

The dry run has shown that we have an untracked file named example_untrackedfile. We need to specify the force option with git clean to remove it. In the example below, it will only clean the untracked files in the current directory.

$ git clean -f

If untracked files are in another directory, the syntax is:

$ git clean -f parent-dir/child-dir/

We can confirm if the untracked file was removed by running the git status.

From the output, we now have one untracked directory. The untracked file was forcefully removed.

3. Deleting Untracked Directory

The git clean -f command doesn’t delete untracked directories by default. However, adding the -d flag will do the magic.

Furthermore, like in the case of files, you need first to see which untracked directories will get deleted. To do so, use the command:

$ git clean -dn

In our case, we see that we have one untracked directory.

Once you’ve confirmed so, proceed to delete the directory using the command:

$ git clean -df

Proceed to confirm the deletion by running the git status. The output should now show no untracked files and directories.

4. Git Clean Interactive Mode

Using the -i flag activates the interactive mode, which is helpful to see what is happening before you delete the files. A what now prompt will display, requiring you to enter a command to proceed with any action.

For instance, to see the untracked directories using the interactive mode, run the command:

$ git clean -dni

You can click any command to proceed, such as 5, then click the enter button to quit the prompt.

If you are unsure what each command in the prompt represents, choose option 6 to open the help page.

5. Deleting Ignored Files

The -f flag omits ignored files. Some .gitignore files are non-crucial files generated during compilation. If after you’ve done a “dry-run” you feel you need to delete them, add the -x flag when deleting folders or files. For instance, to delete ignored files and untracked directories simultaneously, use the command:

$ git clean -dfx

Let’s create a .gitignore file in our workspace and use the -f to delete it.

To create the file, do the following.

$ touch ignorefile1.txt
$ echo ignorefile1.txt >> .gitignore

Run the git status to confirm the file.

Next, remove the .gitignore file.

$ git clean -fx

That’s it! You now have the basics of using the git clean command.


Git clean is a powerful command to clean your working space. As a rule of thumb, always “dry-run” before deleting anything. Once deleted, there is no going back. Nevertheless, you’ve added one tool to your git “undo” methods. Keep your workspace clean by using the git clean command to delete untracked directories and files.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.