Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Git ignore my specified file?

People also ask

Why is Gitignore not ignoring my files?

gitignore ignores only untracked files. Your files are marked as modified - meaning they were committed in the past, and git now tracks them. To ignore them, you first need to delete them, git rm them, commit and then ignore them.

Why git ignore is not working?

Check the file you're ignoring Take a good look at your structure, and make sure you're trying to ignore the file that isn't already committed to your repository. If it is, remove the file from the repository and try again. This should fix the Gitignore not working issue.

How do I force git to ignore a file?

Use Git update-index to ignore changes To resume tracking, run the git update-index command with the --no-skip-worktree flag. Or, you can temporarily stop tracking a file and have Git ignore changes to the file by using the git update-index command with the assume-unchanged flag.

Why is git not using Gitignore?

Some times, even if you haven't added some files to the repository, git seems to monitor them even after you add them to the . gitignore file. This is a caching issue that can occur and to fix it, you need to clear your cache.


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.


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

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.


.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.


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.


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