Remove files in .gitignore from version control

If you have a file like xyz which is under version control, then even if you add an entry for the file in .gitignore, git won't automatically remove it from version control. If you have just one file you can do that yourself with

git rm --cached xyz
However, what if you have hundreds of different files which you need to remove? The recipe is
git commit -m "commit message"
to commit the current situation, then remove all the files from git's index and then add them back again with
git rm -r --cached .
git add .
git commit -m ".gitignore is now working"
Note the "." at the end of the lines here. The first command removes everything from the index, then the second command adds everything back again, but this time it reads .gitignore before doing so, so this magically removes everything in .gitignore for you.

See .gitignore file not ignoring.

Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer