Git: Remove file or folder from git tracking

Sometime while working with a project , we come in a situation where we see that some files or folders are being tracked but later we do not want them to be tracked.

It might be possible that while creating .gitignore file we forgot to add those files/folders pattern.

Such examples are : target folder, custom folders lke log,report etc which we add later after we create .gitignore.

So we can tell git to stop tracking for these files for any changes made in future but still want these to remain in out local, so that we can work with them locally without any issues.
The solution is :

Step1: execute below command in git bash or command line while within the same project dir.
This will tell git that these are deleted and git will stop tracking ( Tricking git)

git rm -r --cached path_to_your_folder/
or
git rm -r --cached filename_pattern

Step2: Add those folder/file in .gitignore too.
So that in future even these files or folders being created or mistakenly added, still it will be ignored by git.

Step3: Now commit these changes and push. (1-deletion of excluded folder/file and 2-updation .gitignore)

Check this article if you want to know more about what files to ignore.

Leave a Reply