How to Remove Untracked Files in Git

Ashok Chapagai Feb 02, 2024
  1. Remove Untracked Files From Working Tree Using git clean
  2. Remove Untracked Files Interactively in Git
  3. Remove Untracked Files and Directories
  4. Remove Ignored Files and Directories
How to Remove Untracked Files in Git

Sometimes you might want to remove the untracked files that are of no use, such as the log file created by specific applications. You might want to remove it as it is untracked and might not be of any use. Below is the scenario that may arise while removing untracked files using Git.

Remove Untracked Files From Working Tree Using git clean

The git clean command cleans by removing files that git does not track. It removes all the files recursively and starts removing files from the presently active directory. In normal cases, it just removes files that are unknown to Git. But with the use of the -x flag, it can also remove ignored files.

So, firstly, you can check the status of untracked files as,

git status

The command above will show any files that might be untracked. If you want to track certain files, you can use the following command,

git add <file_name>

Now that you know what files are tracked, you can use various flags to obtain the desired result.

Remove Untracked Files Interactively in Git

In order to list out all the files and directories that will be deleted if you use git clean, you can use the following command.

git clean -n -d

Here, the -n flag (--dry-run) shows the list of files, and the -d flag is used to show directories if any.

Remove Untracked Files and Directories

One we are sure to remove all the untracked files and directories, we can use the following command to remove all the untracked files and directories.

git clean -d -f

Output:

Removing jpt/
Removing main.pyc

It will remove all the untracked files and directories. To remove only the files, we should not use the -d option to remove directories. The -f option is to force removing the untracked files and directories.

To verify all the untracked files and directories are removed, we can use the git clean -d -n to list all the untracked files and directories. If it doesn’t list any files and directories, it means all the untracked files and directories are removed.

We can use -i option along with the git clean command to delete the unracked files and directories interactively.

git clean -d -i

Output:

Would remove the following items:
  jpt/      main.pyc
*** Commands ***
    1: clean                2: filter by pattern    3: select by numbers
    4: ask each             5: quit                 6: help
What now> 

It will prompt us, and further operations will be carried out based on our input.

Remove Ignored Files and Directories

To remove all the untracked and ignored files and directories, we add the -x option to the git clean command.

git clean -d -f -x

To remove only ignored files and directories, we add -X option to the git clean command.

git clean -d -f -X
Ashok Chapagai avatar Ashok Chapagai avatar

Ashok is an avid learner and senior software engineer with a keen interest in cyber security. He loves articulating his experience with words to wider audience.

LinkedIn GitHub

Related Article - Git Tracking