Is there a way to remove all ignored files from a git repo?

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 .


There is a single command solution:

git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached

What it does is:

  • List all ignored files
  • Handle paths with spaces to avoid failure
  • Call git rm -r --cached to remove all the ignored files from index (without removing them from your local machine)

Tags:

Git