Git .ignore not working for certain files

Today git serves as an medium between all the digital apps created daily in the whole world. It helps us in various ways by reducing miles in the developer to production environment. But, for some reason it also acts as a headache in the development stage. So in this thread we discuss about the erros caused by git.

Also read: How to Fix git@gitlab.com: Permission denied

While working on one of my laravel project today (may 26, 2020) i found that git ignore is not working for one of my files wich i already added in the .gitignore file. It is a configuration file for my website and each time i push changes it causes error.

After some research i think i found a solution that working for me. So i decided to share it to my friends.

The Problem

I added the following line to .gitignore:

sites/default/settings.php

but when I type git status it shows the file as unstaged file.

Steps to reproduce

The reason to ignore files in Git is so that they won’t be added to the repository. If you previously added a file you want to be ignored, then it will be tracked by Git and the ignore rules matching it will be skipped. Git does this since the file is already part of the repository.

In the root directory run git status --ignored this will give you an output of ignored files added to .gitignore.

You can confirm this through the output of git status. If the file is not listed in the “Untracked files” section, then it is already tracked by Git and it will ignore the rule from the .gitignore file.

Git .ignore

How to ignore

In order to actually ignore the file, you have to untrack it and remove it from the repository. You can do that by using git rm --cached command below. This removes the file from the repository without physically deleting the file (that’s what the --cached does). After committing that change, the file will be removed from the repository, and ignoring it should work properly.

Type below command in root directory with your own path:

git rm --cached examle/filename.php

Replace the examle/filename.php with your own path to the file.

Note: Sometimes you’ll also need to also do a git add . after git rm --cached to properly rebuild the index.

By Shahas Nizar

A passionate blogger and a tech enthusiast who is always keen and awaiting to know about fully fledged new technologies who's currently using iPhone 12 as a part of daily routine. You can spot me @ #facebook, #twitter & #Google +

Leave a Reply