git add adding ignored files

后端 未结 10 2163
不知归路
不知归路 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:30

    I've found a way to add files to .gitignore even if they have already been commited once. It's a bit brutal but it works great.

    1. Add the different files to your .gitignore

    2. Move these files out from your git project

    3. Rescan & StageChanged, these operation will tell you that these files have been deleted

    4. Commit

    5. Move back your files to your project, they won't be tracked anymore !

    0 讨论(0)
  • 2021-02-01 01:38

    I created a repository to try and duplicate your issue, and I got the first answer from http://www.stackoverflow.com/questions/11451535/gitignore-not-working to work.

    Here's my repo if you are curious: https://github.com/IAMZERG/so_project_gitignore

    Try adding this to the .gitignore instead:

    **/directory_to_remove

    After that, run git rm --cached directory_to_remove -r

    git status should show that you deleted a bunch of files in the directory you are trying to remove. Then, commit and push to your remote, and everything should be golden... Maybe?

    0 讨论(0)
  • 2021-02-01 01:41

    Add the file to git ignore, then

    git update-index --assume-unchanged <file>
    
    0 讨论(0)
  • 2021-02-01 01:41

    I had a similary issue. Making sure my encoding was ANSI and the line endings were Unix(LF) fixed my issue.

    0 讨论(0)
  • 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 <file>..." 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.

    0 讨论(0)
  • 2021-02-01 01:42

    Another approach if you don't want to use git rm --cached

    rm -Rf node_modules
    git add -u
    git commit -m "stop tracking node_modules"
    
    npm install
    # done
    

    Also note the distinction between node_modules and node_modules/ which you seem to have correct. (Thanks umläute for the note on this)

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