我刚刚在我的新项目的根上做了一个git初始化。
然后我创建了一个.gitignore文件。
现在,当我输入git status时,.gitignore文件出现在未跟踪文件列表中。为什么呢?
我刚刚在我的新项目的根上做了一个git初始化。
然后我创建了一个.gitignore文件。
现在,当我输入git status时,.gitignore文件出现在未跟踪文件列表中。为什么呢?
当前回答
首先,正如许多人已经说过的,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:
*
这个接缝工作得很好,直到您意识到目录没有添加到存储库(正如预期的那样)。这样做的原因是.gitignore也将被忽略,因此目录是空的。因此,你应该这样做:
*
!.gitignore
最终用户很可能希望Git忽略”。gitignore”文件,因为Eclipse创建的IDE特定文件夹可能与NetBeans或其他IDE不同。因此,为了保持源代码与IDE的对立性,很容易忽略一个自定义git,因为每个开发人员可能使用不同的IDE,而不与整个团队共享。
你也可以有一个全局用户git .gitignore文件,它会自动应用到你所有的回购。这对于IDE和编辑器文件很有用(例如,Vim的swp和*~文件)。更改目录位置以适合您的操作系统。
添加到~/。gitconfig文件: (核心) Excludesfile = /home/username/.gitignore 创建一个~/。Gitignore文件,文件模式被忽略。 将您的点文件保存在另一个repo中,以便您有备份(可选)。
任何时候你复制,初始化或克隆一个repo,你的全局gitignore文件也将被使用。
我发现忽略烦人的. ds_store文件的最佳位置是在.git/info/exclude文件中。
当您在IntelliJ中设置git存储库时,它似乎会自动执行此操作。
以防别人和我们有同样的痛苦。 我们想要排除一个已经提交的文件。
这篇文章更有用: 使用.git/info/exclude太晚了
具体来说,你需要忽略一个文件实际上是使用命令git remove 参见git rm (http://www.kernel.org/pub/software/scm/git/docs/git-rm.html)
你通过去测试它
Git rm——dry-run *.log (如果你说想要排除所有日志文件)
这将输出如果运行它将被排除的内容。
then
你通过运行它
Git rm *.log (或任何你想要的文件名路径/表达式)
然后在.gitignore文件中添加*.log行。