Git

How to remove untracked files

Git user works with three types of files in the git repository. These are tracked files, untracked files, and ignore files. The files which have been added and committed in the repository are called tracked files. The other files of the repository that are not ignored files are called untracked files. Suppose the untracked files are not necessary for the repository. In that case, it is better to remove the unnecessary untracked file and make the working directory clean. `git clean` command is used to remove the untracked file from the current working directory. After executing this command, the removed file can’t be recovered. So, it is better to keep the backup of the repository before executing `git clean` to prevent the accidental deletion of the necessary files. The ways to remove the untracked files from the repository using the `git clean` command as shown in this tutorial.

`git clean` command options:

Option Purpose
-d When the path is not defined with the `git clean` method, then the untracked directories will not be removed. The -d option is used to remove the untracked directories of the repository also. But if the path is defined with the command, then all the untracked files of the defined path will be removed, and no need to use the -d option.
-f, –force If the value of the clean.requireForce is set to True in the git configuration settings, then the `git clean` command will delete the files or directories forcefully with the -f option.
-i, –interactive It is used to display multiple options for the users to delete the untracked files.
-n, –dry-run It is used to display the users which files will be removed but don’t remove any file.
-q, –quiet It is used to report errors.
-e <pattern>, –exclude=<pattern> It is used to ignore files by excluding patterns, and the standard ignores rules defined in the .gitignore file.
-x It is used to ignore the files by the pattern given with -e options from the command line.
-X It is used to remove the files ignored by Git only.

Remove Untracked Files:

You can create a new local repository or any existing repository to check the commands used in this part of this tutorial. I have used an existing local repository named PHP and opened the repository folder from the terminal. Run the following command to check the current status of the repository.

$ git status

The following output shows that four untracked files are not added to the repository.

Run the following ` git clean -d -n` command to check which files will be deleted after executing the command. The use of -d and -n options has been explained before.

$ git clean -d -n

The following output shows that five untracked files will be removed when the `git clean` command is executed with the option to remove the untracked files forcefully.

Run the following command to remove one or more untracked files by using interactive options. Six options will be appeared for the users after executing the command. The first option is used to remove all untracked files from the current directory. The second option is used to remove one or more untracked files based on the pattern. The third option is used to remove one or more untracked files based on the file number. The fourth option is used to remove untracked files by asking permission from the user. The fifth option is used to quit from the command without deleting any file. The sixth option is used to get information about the `git clean` command in interactive mode.

$ git clean -d -i

The following output shows that option 5 is selected to terminate from the command.

Run the above command again to check the use of the fourth option. The user can type 4 or a to select this option. The following output shows that ‘y’ is pressed for the echo3.php file only, and this file has been removed only. Next, the `git status` command shows the list of untracked files after delete.

Run the above command again to check the use of the third option. The user can type 3 or s to select this option. The following output shows that file number 2 is selected to remove the echo2.php file. To delete multiple files, the file numbers have to set as a range such as 1-3. Next, the `git status` command shows the list of untracked files after delete.

Run the above command again to check the use of the second option. The user can type 2 or f to select this option. The following output shows that ‘*.html’ is typed as the pattern to delete all files without the extension ‘.html’ and echo1.php file has removed here. Next, the `git status` command shows the list of untracked files after delete.

Create a folder named temp in the current repository and run the following commands to check the current status of the repository, remove the temp folder by defining the folder path with the `git clean` command and check the status again after delete.

$ git status
$ git clean -d -n temp
$ git status

The following output shows that the temp folder has been deleted from the current repository.

Next, run the following commands to check the current status of the repository before and after deleting all untracked files from the current repository forcefully with the -f option.

$ git status
$ git clean -d -f
$ git status

The following output shows that all untracked files have been removed from the current repository, and the working directory is clean now.

Conclusion:

The uses of the `git clean` command in different ways to remove untracked files from the git repository have been shown in this tutorial by using a demo repository. The untracked files have been removed by using the interactive option and force option here. I hope the reader will delete the untracked file from the local repository after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.