I added the following line to .gitignore
:
sites/default/settings.php
but when I type git status
it shows the file
I run into this, it's an old question, but I want that file to be tracked but to not track it on certain working copies, to do that you can run
git update-index --assume-unchanged sites/default/settings.php
Another possible reason – a few instances of git clients running at the same time. For example "git shell" + "GitHub Desktop", etc.
This happened to me, I was using "GitHub Desktop" as the main client and it was ignoring some new .gitignore settings: commit after commit:
Reason: the Visual Studio Code editor was running in the background with the same opened repository. VS Code has built-in git control, and this makes some conflicts.
Solution: double-check multiple, hidden git clients and use only one git client at once, especially while clearing git cache.
I tried most commands above on VS Code terminal and I got errors like:
fatal: pathspec '[dir]/[file]' did not match any files
I opened the project on GitHub Desktop and ignored from there and it worked.
Make sure that your .gitignore
is in the root of the working directory, and in that directory run git status
and copy the path to the file from the status output and paste it into the .gitignore
.
If that doesn’t work, then it’s likely that your file is already tracked by Git. 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.
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 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 sites/default/settings.php
. 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.
One thing that I think has been missed in the excellent answers here is the existence if another *.gitignore* or multiple of them.
In a case, I had cloned a repo and could not figure out why a "ignored" file was being added again when running git add. It worked out that there was another .gitignore in a subfolder and that was overriding the one in the root folder.
/.gitignore
/some-folder/.gitignore
/some-folder/this-file-keeps-getting-staged
The
root /.gitignore
was ignoring this-file-keeps-getting-staged
The
/some-folder/.gitignore
did not have a specification to ignore "this-file-keeps-getting-staged" file.
And hence the issue.
One of the things is to simply search for multiple .gitignore files in the hierarchy. And figure out the rules as they apply.
What I did it to ignore the settings.php file successfully:
I think if there's the committed file on Git then ignore doesn't work as expected. Just delete the file and commit. Afterwards it'll ignore.