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.

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

63 Followers

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com