How to Start Ignoring Already Tracked Files in Git with .gitignore and git rm — cached command

Pavol Kutaj
1 min readMay 3, 2021

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.

steps

  1. add the file to .gitignore
  2. run git rm --cached <file>
  3. if you want to remove a whole folder, run git rm -r --cached <folder>
  4. 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

links

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com