How to Start Ignoring Already Tracked Files in Git with .gitignore and git rm — cached command
The aim of this explainer💡/how-to-guide🏁 is to add items
.gitignore that have already been tracked. Usually, to ignore a file you just add it to
.gitignore and you never stage/commit it. But you may change your mind or you may stage/commit in with
git add ., etc.
- add the file to
git rm --cached <file>
- if you want to remove a whole folder, run
git rm -r --cached <folder>
- commit the change
- The activation of the change / the removal of the file/folder from the HEAD revision will happen on the next commit which contains the deleted file (in index only). From then on, it starts to be ignored again
- this will not remove the physical file from your local repo
- NOTE: this does remove the files from other developers machines on next
- as for
When — cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.
— cached Use this option to unstage and remove paths ONLY FROM THE INDEX. Working tree files, whether modified or not, will be left alone.
— from Git — git-rm Documentation