What are Untracked Files – The files are folders which is created locally and have not been added to the git repository ever.
Sometimes you may need to delete these untracked files from your code. These files can be some temporary created files in application. Use the below commands to list all untracked files and delete them from code base.
List Untracked Files –
First list all the untracked files using following commands. This is the list of files which will be deleted. This is to make sure that you are not deleting any useful files.
$ git clean -f -n
Remove Untracked Files –
All the files listed in above output will be deleted completely from system. You can’t recover these files back. Check all files listed above and if found any useful files add them in repository first. After confirming that all listed files are not useful, now execute following command to delete them.
$ git clean -f
Remove Untracked Directories Also –
Above command will delete all untracked files only but not directory. So if you want to remove untracked directories also, use the following commands. First command will list untracked files and directories and second command will delete them.
$ git clean -f -d -n $ git clean -f -d
See the git-clean docs for more information.