git添加添加被忽略的文件 [英] git add adding ignored files

查看:987
本文介绍了git添加添加被忽略的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从git中删除以前跟踪的目录,该目录可以正常工作,但它会随后添加每个 git add。 git add -A 等等。以下是我所做的:



添加到项目根目录中的.gitignore:

  node_modules 

运行如下所示:

  git rm -r --cached node_modules 
git commit -a -mremoved node_modules
git push origin master

到目前为止,这样做会将目录从远程存储库中删除。问题是,当我以后运行 git status 时,它告诉我node_modules目录未被跟踪,并且在将来的提交中继续添加它。



我错过了什么以及/或者如何找到问题的根源?

From here b git add命令默认不会添加被忽略的文件。
...
使用git add命令可以使用-f(强制)选项添加被忽略的文件。

来自评论的其他信息

我正在跟踪.gitignore文件。



code> git check-ignore node_modules / 返回node_modules /按预期方式。



不使用子模块。 p>

更新:

我已经创建了一个似乎复制以下问题的示例上述步骤:

https://github.com/ awhitehouse104 / SampleRepo



解决方案: 总结答案并从下面的评论,这个问题是在我的.gitignore文件的编码。我已经使用 echo'node_modules'> .gitignore 在Windows 8上创建文件,并以BOM形式显示为UTF-16(根据下面的答案)。经过几次谷歌搜索之后,似乎这是powershell的默认编码,我可以确认以UTF-8保存似乎已经解决了问题。



tldr; 或许不要使用这种方法创建.gitignore文件或准备更改编码

echo'node_modules' > .gitignore

解决方案

您可能有一个否定规则以开头,位于 node_modules .gitignore 文件中$ c $ line。
$ b $ p $ g $ check-ignore 在文档中存在一个错误/含糊不清的问题。您希望如果 git check-ignore node_modules / 打印 node_modules / ,那么 node_modules / 被忽略。但实际上,如果该路径名与任何忽略模式匹配,则它会打印路径名 - 正值或负值。唯一可以肯定的方法是使用 -v - verbose )选项,这将使 git check-ignore 打印匹配模式。

此外,如果 git check-ignore -v 表示一个目录被忽略,这并不一定意味着该目录中的所有文件都被忽略。示例回购:

  / 
.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 =>忽略

$ git check-ignore -v node_modules / foo
.gitignore:2:!/ node_modules / foo node_modules / foo
^ negative pattern =>不被忽略

$ git add -A

$ git status
#在分支主机上
#要提交的更改:
#(使用git reset HEAD< file> ...来取消)

#new file:node_modules / foo

因此,如果 git check-ignore -v node_modules / 表示 node_modules / 被忽略,执行 git add -A node_modules / ,然后运行 git check-ignore -v --no-index


$ b

更新:code>与添加的单个文件相比较,以发现添加的原因。

我没有想到:您的 .gitignore 文件位于带BOM(字节顺序标记)编码的UTF-16中:

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

这就是为什么git可能无法处理它。以不含BOM的UTF-8保存文件,这将解决问题。但是我也建议在 git check-ignore 中提交bug报告 - 在这种情况下,它的输出显然与git实际忽略的不一致。


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. Here's what I've done:

Add to .gitignore in root of project:

node_modules

Run the following:

git rm -r --cached node_modules
git commit -a -m "removed node_modules"
git push origin master

So far so good, this removes the directory from the remote repository. The problem is when I later run git status it tells me the node_modules directory is untracked and keeps adding it back on future commits.

What am I missing and/or how do I find the root of my problem?

From here:

The git add command will not add ignored files by default. ... The git add command can be used to add ignored files with the -f (force) option.

Additional information from comments:

I am tracking .gitignore file.

git check-ignore node_modules/ returns node_modules/ as expected.

No use of submodules.

Update:

I've created a sample that appears to replicate the issue following the steps above:

https://github.com/awhitehouse104/SampleRepo

Resolution:

To summarize the answer and comments from below, the issue was in the encoding of my .gitignore file. I had used echo 'node_modules' > .gitignore to create the file on windows 8 and it came out as UTF-16 with BOM (according to answer below). After a few google searches, it seems this is the default encoding with powershell and I can confirm that saving as UTF-8 seems to have resolved the issue.

tldr; Probably don't use this method of creating .gitignore files or be prepared to change the encoding

echo 'node_modules' > .gitignore

解决方案

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.

这篇关于git添加添加被忽略的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆