Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to remove all ignored files from a local git working tree?

Tags:

git

Yesterday I spent several hours debugging a problem with my git repo that wasn't fixed by git reset HEAD --hard because the files causing the problem were ignored by .gitignore. Is there a way to "flush" or "clean" a git repo of all files that are being ignored, so that only the files tracked by git are present?

I finally fixed my problem by deleting the repo and cloning it from github again, but in the future, I would like to immediately remove all potentially problematic files (those that are being ignored).

like image 742
Patrick Kenny Avatar asked Sep 18 '17 06:09

Patrick Kenny


People also ask

Does git clean remove ignored files?

The git clean command also allows removing ignored files and directories.


1 Answers

git clean -dfX 

git-clean - Remove untracked files from the working tree
-d for removing directories
-f remove forcefully
-n Don’t actually remove anything, just show what would be done.
-X Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

If the ignored files are already added to the index/staging, you must remove the files from the tracking index before using the above clean command.

git rm -rf --cached .

Then add the files except the ones mentioned in the .gitignore file

git add .

like image 169
Samuel Robert Avatar answered Sep 22 '22 17:09

Samuel Robert