当我运行git add .
时它正在添加目录中的所有文件,即使我在 .gitignore
中指定了它忽略特定的文件夹和文件。在本例中,它是 __venv__
和__pycache
。 .gitignore
在启动 git 存储库之前,会创建文件并指定要忽略的目录。我删除了git
文件夹并从项目的根目录重新启动存储库多次,但它仍然不会忽略 .gitignore
中的声明文件。在 Windows 上,删除并重新启动存储库可以解决问题,但在 Linux 上则不然
更新:将要排除的目录添加到 .git/info/exclude 有效,但将其添加到 .gitignore 时却不起作用
最佳答案
在 .gitignore 中添加的规则应该是:
__venv__/
__pycache/
# or
__pycache__/
(参见 https://www.gitignore.io/api/python )
注意结尾的“/
”
I just realized that my text editor for some reason append a space in front of the declarations automatically.
I found that out after clearing the file and rewrite it again
这可以解释为什么 .gitignore
规则确实没有应用。
如果这两个文件夹中的文件尚未被跟踪,则不会添加它们。
如果没有:
git rm --cached -r __venv__/
关于git 添加 .正在添加目录中的所有内容,即使在 gitignore 中指定了该目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51093846/