How To Remove Untracked Files With Git Clean?

Tim Mouskhelichvili
Tim Mouskhelichvili
3 minutes to read

Git is the most popular version control software in the world, that's why every developer needs to know how to use it and understand it well. Even though mastering Git takes time, it is crucial to learn its most popular commands. One of those commands is the git clean command.

git clean removes untracked files from a working tree of a git repository.

In this article, I will explain how and when to use this git command, show different options that you can use to modify its behavior, as well as explain how the interactive mode works.

Let’s get to it 😎.

git clean

The definition

git clean is a git command that removes untracked files from a working tree.

To run this command, you will need to open a terminal from the location of your git repository and type:

bashgit clean -n

Using the -n option lets us preview the changes before doing the actual removal of the untracked files.

If you want to proceed with the removal of the untracked files, you can use the force -f option.

However, be very careful, this command will delete files for REAL.

bashgit clean -f

To verify the removal of the untracked files, use the git status command.

This command will only touch untracked files OR directories.

How to delete untracked directories?

By default, git clean will not touch directories. This is done to prevent accidental permanent deletion.

If you want to delete untracked directories you will need to use the -d option.

First, let's use it with the preview -n option:

bashgit clean -dn

This command will output the directories up for removal.

Once you are sure about the removal, use the force -f option.

bashgit clean -df

How to delete ignored files?

By default, git clean will not touch files specified inside the .gitignore file.

If you want to delete ignored files you will need to use the -x option.

First, let's use it with the preview -n option:

bashgit clean -xn

This command will output the files up for removal.

Once you are sure about the removal, use the force -f option.

bashgit clean -xf

The interactive mode

Using the interactive mode, you will be able to choose exactly what to do with each file.

You can activate the interactive mode using the -i option.

bashgit clean -i

This will output a questionnaire and the 'What now' prompt, where you can select the option that you want.

Here is an example of an output that you can expect when using the interactive mode.

bashWould remove the following item:
  app/toRemove.tsx
*** Commands ***
    1: clean 
    2: filter by pattern    
    3: select by numbers    
    4: ask each             
    5: quit                 
    6: help
What now> 

Here is a table with all the options that you can select from in interactive mode:

OptionDescription
cleanIt will delete the indicated files.
filter by patternIt will show a screen where you will be able to filter by pattern the files to choose.
select by numbersIt will show a screen where you will be able to select by numbers the files to choose.
ask eachIt will iterate and ask a yes/no prompt on each file.
quitIt will exit the interactive mode.
helpIt will show a screen that explains all the commands that you can choose from.

You can combine the options when using the interactive mode.

For example:

  • if you want to include untracked directories, use the -di option.
  • if you want to include ignored files, use the -xi option.

Final thoughts

Well, now you understand the git clean command and from now on will be able to use it in your day-to-day developments life 🥳.

But, you need to be careful while using it, because the removed files are untracked and you will not be able to restore them. You may want to consider using the git stash command instead.

git fetch vs pull

I have written more tutorials on git if you are interested in learning more about it and its commands.

If you have any questions please ask them in the comments. It will be a pleasure for me to answer them.

Thank you for reading through this article, please share it with your fellow developers.

Comments (0)
Reply to: