git .gitignore 不忽略 node_modules

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42049390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:35:47  来源:igfitidea点击:

.gitignore not ignoring node_modules

git

提问by Rimil Dey

Even after adding the .gitignorefile to the root of the .gitrepository, I cannot make git ignore the node_modulesdirectory.

即使将.gitignore文件添加到.git存储库的根目录后,我也无法让 git 忽略该node_modules目录。

The files have not been added for Git to track.

这些文件尚未添加以供 Git 跟踪。

I have gone through earlier stack overflow questions, and also tried adding a comment line to the first line, since apparently Git doesn't read the first line of that file, and it still doesn't work. I have tried to use the following command also, with no avail:

我已经解决了早期的堆栈溢出问题,并尝试在第一行添加注释行,因为显然 Git 没有读取该文件的第一行,而且它仍然不起作用。我也尝试使用以下命令,但无济于事:

git rm --cached -r .

Could anyone help me out? The content of .gitignore:

有人可以帮我吗?内容.gitignore

#first line
node_modules/

采纳答案by janos

Even after adding the .gitignore file to the root of the .git repository, I cannot make git ignore the node_modules directory.

即使将 .gitignore 文件添加到 .git 存储库的根目录后,我也无法让 git 忽略 node_modules 目录。

The .gitignorefile should be placed in the root of the working tree (also known as the root of your project), which is just above the .gitdirectory. Inside the .gitdirectory the file has no effect.

.gitignore文件应放置在工作树的根目录(也称为项目的根目录)中,该.git目录位于目录的正上方。在.git目录中,该文件无效。

This makes sense, because normally you want to put this file under version control, to share the list of ignored files with all developers on the project. And only the files inside the working tree, outside .gitare under version control, the .gitdirectory is for Git's internal storage.

这是有道理的,因为通常您希望将此文件置于版本控制之下,以便与项目的所有开发人员共享被忽略文件的列表。并且只有工作树内部的文件,外部的文件.git处于版本控制之下,该.git目录用于Git内部存储。

(If you wanted to ignore patterns only locally, without adding to version control, you could do so by defining patterns in .git/info/exclude.)

(如果您只想在本地忽略模式,而不添加到版本控制,您可以通过在.git/info/exclude.

[...] and also tried adding a comment line to the first line, since apparently Git doesn't read the first line of that file

[...] 并且还尝试在第一行添加注释行,因为显然 Git 没有读取该文件的第一行

For the record, it does read the first line too, there's nothing special about the first line in .gitignore.

作为记录,它也读取了第一行,.gitignore.

回答by janos

I solved the problem by deleting the file name at .gitignoreand made it an invisible file respectively a dot-file, just like naming the file .gitignoreand keeping the expression

我通过删除文件名解决了问题,.gitignore并使其成为一个不可见的文件,分别是一个点文件,就像命名文件.gitignore并保留表达式一样

node_modules/

node_modules/

in that file, so that works and now git ignores finally the node-modules directory. Don't know exactly, why this is working, but at least it does.

在那个文件中,这样就可以了,现在 git 最终忽略了 node-modules 目录。不确切知道为什么这有效,但至少确实如此。

回答by Stanislau Buzunko

In my case i had it written with quotation marks like this

就我而言,我用这样的引号写了它

'functions/node_modules/'

instead of

代替

functions/node_modules/

回答by guneysus

In my case, solved by converting file encoding from 'Unicode' to 'UTF-8 Without BOM' since I created .gitignorefile via Powershell by redirecting output.

就我而言,通过将文件编码从“Unicode”转换为“UTF-8 without BOM”来解决,因为我.gitignore通过 Powershell 通过重定向输出创建了文件。

Initially I changed line ending from Windows (CRLF) to Linux (LF), but did not help.

最初我将行尾从 Windows (CRLF) 更改为 Linux (LF),但没有帮助。