How to Ignore Tracked Files with .gitignore
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 with git add .
, etc.
1 min readMay 3, 2021
steps
- add the file to
.gitignore
- run
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
notes
- this will not remove the physical file from your local repo
- NOTE: this does remove the files from other developers machines on next
git pull
- as for
git rm
:
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