The Git clean command can be used to remove the untracked files as follows:Forcefully deleting untracked files command:
For interactively deleting files, use the -i option:
The above command displays the files that will be removed and gives options to choose (see examples in the next section).
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?
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:
main.o
mygame.o
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:
Out of these, the footer.php, haeder.php and README.md are added to the repository or being tracked. This can be confirmed by running the following command:
The result should be:
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:
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
Suppose, we have tst3.txt and tst4.txt files in our repository folder. Running the following command:
The result:
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.
After running the clean command:
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:
This command displays the untracked files and directories to be removed (so giving you a chance to review).
Similarly, to remove untracked folders and files immediately, you may also use this command:
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:
Displays the files to be removed.
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:
It results in the following:
You can see six options that are self-descriptive.
If you enter option 1 or c, it will remove all untracked files.