I added .gitignore to my project after I had everything commited. Now I want to run a command like:
git rm --cached *everything_listed_in_gitignore*
How can this be achieved? Thank you in advance.
On
I found another way that works even better in any operating system from https://ardalis.com/how-to-make-git-forget-tracked-files-in-gitignore .
After adding new rules to .gitignore
git rm -r --cached .
git add .
Basically, it removes everything and adds everything back. When adding, Git will ignore files based on .gitignore
On
There is a shorter version, the answer is taken from here.
You can also remove files from the repository based on your .gitignore without deleting them from the local file system:
git rm --cached `git ls-files -i -X .gitignore`
Or, alternatively, on Windows Powershell:
git rm --cached $(git ls-files -i -X .gitignore)
I'm always using the following line to remove files listed in my
.gitignore:This will find lines in
.gitignorethat do not match (-voption to grep) the regular expression^(#|\$)matching lines that start with#(comments) or empty lines.Then
sedwill remove a forward slash/at the start of the line.The results are passed to
rm -fusingxargs, which you could replace withgit rm --cachedNote:
Your
.gitignorefile can contain entries like*.tmpto ignore all.tmpfiles throughout the project. The above command would only remove*.tmpfiles from the root. Only full paths will be handled correctly.