Removing the tracking of files, already tracked by Git

If you work on Git then you must be knowing once we fork(or initialise a repository) a repository, all the files inside that folder get tracked automatically by Git. Well, thats not a problem if we really want all of them to be tracked all the time. But the problem starts when there are some files we need not track. And the worse happens when all of the files are being tracked and now we decide to remove some files from being tracked. The handy command is

$ git rm --cached readme.txt

And for folders

$ git rm --cached docs/

There is an option to set a rule for Git to ignore all the files, one need Git to ignore from tracking. This rule is set in a file named .gitignore. Remember there is a dot (.) before gitignore word name. All those files which need to be ignored must be mentioned in this .gitignore file. The tricky part is .gitignore will not remove files from tracking by Git if those files are already being tracked by Git. In those case the above “git rm –cached” trick will help.
The way I do it regularly is add the .gitignore file in the beginning inside the folder I am going to initialise Git repository. That only works if I know which files I need to ignore in the beginning only.
There is a set of .gitignore templates for different languages at Github, That is a great help in the beginning of configuring the .gitignore for different languages.

4 thoughts on “Removing the tracking of files, already tracked by Git

  1. very nice. it would also be helpful to mention that you can add folders/files etc to be ignored one per line in your .gitignore

    • hey Peter,
      thanks for dropping the lines here.
      Very well said, that clears up the configuration things a little better.

Comments are closed.