git add adding ignored files

后端 未结 10 2264
不知归路
不知归路 2021-02-01 00:58

I\'m trying to remove a previously tracked directory from git, which works, but it\'s being added back with each subsequent git add ., git add -A, etc.

10条回答
  •  野性不改
    2021-02-01 01:42

    You probably have a negative rule (include-again rule, the one that starts with an !) in your .gitignore file somewhere after the node_modules line.

    git check-ignore has a bug/ambiguity in the docs. You expect that if git check-ignore node_modules/ prints node_modules/, then node_modules/ is ignored. But actually it prints a pathname if that pathname matches any ignore pattern - positive or negative. The only way to be sure is to use the -v (--verbose) option, which will make git check-ignore print the matching pattern.
    Moreover, if git check-ignore -v says a directory is ignored, it doesn't necessarily mean that all files in that directory are ignored. Example repo:

    /
        .git/
        .gitignore
        node_modules/
            bar
            foo
    
    $ cat .gitignore 
    /node_modules/*
    !/node_modules/foo
    
    $ git check-ignore -v node_modules/
    .gitignore:1:/node_modules/*    node_modules/
                 ^ positive pattern => ignored
    
    $ git check-ignore -v node_modules/foo
    .gitignore:2:!/node_modules/foo node_modules/foo
                 ^ negative pattern => not ignored
    
    $ git add -A
    
    $ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD ..." to unstage)
    #
    #   new file:   node_modules/foo
    #
    

    So if git check-ignore -v node_modules/ says node_modules/ is ignored, do git add -A node_modules/ and then run git check-ignore -v --no-index against individual files that got added, to discover why they were added.


    Update: I didn't expect that: your .gitignore file is in "UTF-16 with BOM (byte order mark)" encoding:

    $ cat .gitignore | hexdump -vC
    00000000  ff fe 6e 00 6f 00 64 00  65 00 5f 00 6d 00 6f 00  |..n.o.d.e._.m.o.|
    00000010  64 00 75 00 6c 00 65 00  73 00 0d 00 0a 00        |d.u.l.e.s.....|
    

    That's why git probably can't handle it. Save the file in UTF-8 without BOM, that should fix the problem. But I also suggest filing a bug report against git check-ignore - in this corner case its output is clearly not consistent with what git actually ignores.

提交回复
热议问题