What are Untracked Files – All the files and directories, 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 temporarily created files in an application. Use the below commands to list all untracked files and delete them from the code base.
List Untracked Files
First list all the untracked files using the 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 the 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 the following command to delete them.
git clean -f
Remove Untracked Directories
Above command will delete all untracked files only but not directory. So if you want to remove untracked directories also, use the following commands. The first command will list untracked files and directories and the second command will delete them.
git clean -f -d -n # List untracked files and directories git clean -f -d # Remove untracked files and directories
See the git-clean docs for more information.