Using Git Clean command for removing untracked files in Git
The Git clean command can be used for removing the untracked files as follows:
Forcefully deleting untracked files command:
$ git clean -f
For interactively deleting files, use the -i option:
$ git clean -i
The above command displays the files that will be removed and gives options to choose (see examples in the next section).
$ git clean –n
Only displays what will be removed by the clean command. It is useful if you are unsure about the file/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?
A file is tracked if it is under the Git (or any other VCS) or exists in the Git index. On the other hand, untracked files are not controlled by the version control system.
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 into 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 wanted 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 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 in the repository or being tracked. This can be confirmed by running the following command:
$ git ls-files
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 that we want o delete. For immediately deleting both files, run this command:
$ git clean -f
The result should be:
1 2 3 |
Removing tst1.txt Removing tst2.txt |
If you look at the folder again, it should not display the text files anymore.
Using interactive delete option
The clear command with –i / –interactive option lets you view files before actually deleting them from the file system.
Suppose, we have tst3.txt and tst4.txt files in our repository folder. Running the following command:
$ git clean –i
The result:
You can see, the 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 enable deleting the files in 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:
$ git clean -d -i
This command displays the untracked files and directories to be removed (so giving you chance for reviewing).
Similarly, for removing untracked folders and files immediately, you may also use this command:
$ git clean -d –f
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 as running these commands:
$ git clean –n
Displays the files to be removed.
$git clean –d –n
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 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:
$ git clean –d –i
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 files that are untracked.