'Why doesn't Git ignore my specified file?
I added the following line to .gitignore:
sites/default/settings.php
but when I type git status it shows the file as unstaged file.
What's the problem? All other patterns work well.
Solution 1:[1]
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
Solution 2:[2]
Please use this command
git rm -rf --cached .
git add .
Sometimes .gitignore files don't work even though they're correct. The reason Git ignores files is that they are not added to the repository. If you added a file that you want to ignore before, it will be tracked by Git, and any skipping matching rules will be skipped. Git does this because the file is already part of the repository.
Solution 3:[3]
.gitignore will only ignore files that you haven't already added to your repository.
If you did a git add ., and the file got added to the index, .gitignore won't help you. You'll need to do git rm sites/default/settings.php to remove it, and then it will be ignored.
Solution 4:[4]
I had the same problem.
Files defined in .gitingore where listed as untracked files when running git status.
This was because the .gitignore file was saved in UTF-16LE encoding, and not in UTF8 encoding.
After changing the encoding of the .gitignore file to UTF8 it worked.
Solution 5:[5]
There are instances e.g. Application Configuration files, which I want tracked in git (so .gitignore will not work), but that I need to change for local settings. I do not want git to manage these files or show them as modified. To do this I use skip-worktree:
git update-index --skip-worktree path/to/file
You can confirm files are skipped by listing files and checking for lines starting with S for skipped
git ls-files -v | grep ^S
If in the future you want to have git manage the file locally again simply run:
git update-index --no-skip-worktree path/to/file
Mescalito above had a great answer, that led me down the right track but
git update-index --assume-unchanged file/to/ignore.php
Has a contract with git that in which : the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index.
However, I change the content of the files, so in my case --skip-worktree is the better option.
Toshiharu Nishina's website provided an excellent explanation of skip-worktree vs assume-unchanged: Ignore files already managed with Git locally
Solution 6:[6]
What I did it to ignore the settings.php file successfully:
- git rm --cached sites/default/settings.php
- commit (up to here didn't work)
- manually deleted sites/default/settings.php (this did the trick)
- git add .
- commit (ignored 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.
Solution 7:[7]
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:
- You commit something.
- Next, commit: it ignores .gitignore settings. Commit includes lots of temp files mentioned in the .gitignore.
- Clear git cache; check whether .gitignore is UTF8; remove files -> commit -> move files back; skip 1 commit – nothing helped.
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.
Solution 8:[8]
Make sure the .gitignore does not have a extension!! It can't be .gitignore.txt, in windows just name the file .gitignore. and it will work.
Solution 9:[9]
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.
Solution 10:[10]
One thing that I think has been missed in the excellent answers here is the existence of 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.
Solution 11:[11]
I just tried this with git 1.7.3.1, and given a structure like:
repo/.git/
repo/.gitignore
repo/sites/default/settings.php
where repo thus is the "root" mentioned above (I would call it the root of your working tree), and .gitignore contains only sites/default/settings.php, the ignore works for me (and it does not matter whether .gitignore is added to the repo or not). Does this match your repo layout? If not, what differs?
Solution 12:[12]
Just in case anyone in the future has the same problem that I did:
If you use the
*
!/**/
!*.*
trick to remove binary files with no extension, make sure that ALL other gitignore lines are BELOW. Git will read from .gitignore from the top, so even though I had 'test.go' in my gitignore, it was first in the file, and became 'unignored' after
!*.*
Solution 13:[13]
One tricky problem is that If you do something like echo node_modules >> .gitignore, it won't work. The windows terminal saves the file in UCS-2 LE BOM and git doesn't seem to accept that.
You can open the file with Notepad and save it with UTF-8 encoding
It Works now.
I think they need to fix this since echo "filetoignore" >> .gitignore actually seems a handy thing to do
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow

