How to Remove Untracked Files in Git

  1. List All the Untracked Files and Directories
  2. Remove Untracked Files and Directories
  3. Remove Ignored Files and Directories

While working on a project, we have many unnecessary files that are self-created or created by mistake. We add such files in .gitignore to make them untracked by Git. To make our repository clean, we need to remove such files. To remove untracked files in Git, we use the git clean command.

List All the Untracked Files and Directories

Before removing all the untracked files and directories, it is better to list all the untracked files and folders because once the files and directories are removed, it’s difficult to restore them.

To view all the untracked files and directories in the current working repository, we can use the git status command.

git status

Output:

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	jpt/
	main.pyc

nothing added to commit but untracked files present (use "git add" to track)

This shows the directory jpt/ and the file main.pyc are untracked.

We can also use the git clean command with the -n option to list all the untracked files and directories that are going to be deleted.

git clean -d -n

Output:

Would remove jpt/
Would remove main.pyc

The -d option is for listing directories that are going to be deleted.

If any of the files or directories listed by the command are important and we want to keep them, we need to either add them to the repository using git add command or list them in .gitignore file.

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
comments powered by Disqus