我刚刚在我的新项目的根上做了一个git初始化。

然后我创建了一个.gitignore文件。

现在,当我输入git status时,.gitignore文件出现在未跟踪文件列表中。为什么呢?


当前回答

最终用户很可能希望Git忽略”。gitignore”文件,因为Eclipse创建的IDE特定文件夹可能与NetBeans或其他IDE不同。因此,为了保持源代码与IDE的对立性,很容易忽略一个自定义git,因为每个开发人员可能使用不同的IDE,而不与整个团队共享。

其他回答

如果你想将被忽略的文件列表存储在Git树之外,你可以使用. Git /info/exclude文件。它只适用于你的回购结帐。

.gitignore是关于忽略其他文件。Git是关于文件的,所以这是关于忽略文件的。然而,当git处理文件时,这个文件需要作为列出其他文件名的机制。

如果它被称为.the_list_of_ignored_files,可能会更明显一些。

一个类比是你不想做的待办事项列表。除非你把它们列在某种“待办事项”清单上,否则你不会知道它们。

如果你已经签入了.gitignore,并且你想忽略对它的修改,看看这个答案:

尝试使用这个命令: git update-index——assume-unchanged FILENAME_TO_IGNORE 要反转它(如果你想提交更改),使用: Git update-index——no-assume-unchanged 更新: 下面是如何列出“假定不变”文件在当前 目录: git ls-files -v | grep -E "^[a-z]" 因为-v选项会用小写字母表示" assume unchanged " 文件。

首先,正如许多人已经说过的,Git应该跟踪你的.gitignore(因此不应该忽略它)。让我来解释一下原因。

(TL;DR:提交.gitignore文件,并使用全局的.gitignore来忽略由IDE或操作系统创建的文件)

您可能已经知道,Git是一个分布式版本控制系统。这意味着它允许您在不同的版本之间来回切换(即使开发已经分散到不同的分支),它还允许多个开发人员在同一个项目上工作。

Although tracking your .gitignore also has benefits when you switch between snapshots, the most important reason for committing it is that you'll want to share the file with other developers who are working on the same project. By committing the file into Git, other contributers will automatically get the .gitignore file when they clone the repository, so they won't have to worry about accidentally committing a file that shouldn't be committed (such as log files, cache directories, database credentials, etc.). And if at some point the project's .gitignore is updated, they can simply pull in those changes instead of having to edit the file manually.

当然,会有一些文件和文件夹您想要忽略,但这是特定于您的,并不适用于其他开发人员。但是,这些不应该出现在项目的.gitignore中。还有两个地方可以忽略文件和文件夹:

Files and folders that are created by your operating system or IDE should be placed in a global .gitignore. The benefit is that this .gitignore is applied to all repositories on your computer, so you don't have to repeat this for every repository. And it's not shared with other developers, since they might be using a different operating system and/or IDE. Files that don't belong in the project's .gitignore, nor in the global .gitignore, can be ignored using explicit repository excludes in your_project_directory/.git/info/exclude. This file will not be shared with other developers, and is specific for that single repository

在我的例子中,我想排除一个现有文件。 仅修改。gitignore无效。 我是这样做的:

git rm --cached dirToFile/file.php
vim .gitignore
git commit -a

通过这种方式,我从缓存中清除了我想要排除的文件,并将其添加到.gitignore。