File added in .gitignore but still shows up on git status, why?

This is a very common scenario for developers. Many cases the developer adds the file in the .gitignore file but the file shows up on git status. Very disturbing and confusing.

If we do a change on the file and do a git status the shows up.

Reason of the issue:

This happens because the file was previously tracked by git. That means the file was added to the git index.

.gitignore will only prevent untracked files from being added. However, git will continue to track any files that are already added to the git index.

To stop tracking a file we need to remove the file from the git index. We can do this using the following command:

git rm --cached file       // In case of a file
git rm -r --cached folder  // In case of a folder

Note: This will NOT remove the physical file from our file system.