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

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

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


当前回答

其思想是将特定于项目的文件放入.gitignore文件中,并(如前所述)将其添加到存储库中。例如.pyc和.o文件,测试套件创建的日志,一些fixture等等。

对于您自己的安装程序创建的但不一定会出现在每个用户面前的文件(例如。swp文件,如果您使用vim,隐藏的ecplise目录等),您应该使用.git/info/exclude(如上所述)。

其他回答

这似乎只适用于当前目录,以便让Git忽略存储库中的所有文件。

更新此文件

.git/info/exclude 

用您的通配符或文件名

* pyc * swp 赶紧*

首先,正如许多人已经说过的,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 update-index——assume-unchanged .gitignore

源。

当然,.gitignore文件会显示在状态中,因为它是未跟踪的,git将它视为一个美味的新文件!

然而,由于.gitignore是一个未跟踪的文件,当你把它放在.gitignore中时,它是一个被git忽略的候选文件!

所以,答案很简单:只需添加一行:

.gitignore # Ignore the hand that feeds!

到你的。gitignore文件!

而且,与August的回答相反,我应该说.gitignore文件不应该在您的存储库中。它只是碰巧可以,这通常是方便的。这可能就是.gitignore被创建为.git/info/exclude的替代品的原因,后者没有被存储库跟踪的选项。无论如何,如何使用.gitignore文件完全取决于您。

作为参考,请查看kernel.org上的gitignore(5) manpage。

导航到git repo的基本目录并执行以下命令:

echo '\\.*' >> .gitignore

所有的点文件都将被忽略,包括讨厌的. ds_store(如果你是在mac上)。