Why doesn't Git ignore my specified file?

前端 未结 13 1325
野性不改
野性不改 2020-11-28 17:12

I added the following line to .gitignore:

sites/default/settings.php

but when I type git status it shows the file

相关标签:
13条回答
  • 2020-11-28 17:54

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

    0 讨论(0)
  • 2020-11-28 17:55

    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.

    0 讨论(0)
  • 2020-11-28 17:56

    I had the same problem. Files defined in .gitingore where listed as untracked files when running git status.

    The reason was that 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 for me.

    0 讨论(0)
  • 2020-11-28 17:56

    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

    0 讨论(0)
  • 2020-11-28 17:57

    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?

    0 讨论(0)
  • 2020-11-28 17:57

    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

    !*.*
    
    0 讨论(0)
提交回复
热议问题