当我运行 git status 时会出现 .gitignore
当我运行 git status 时,.gitignore 会列在未跟踪的文件下。如何防止它被列出?
.gitignore 的内容:
images/builder/
When I run git status
, .gitignore is listed under untracked files. How do I prevent it from being listed?
Contents of .gitignore:
images/builder/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将
.gitignore
添加到您的.gitignore
文件中,但您可能只想提交它,因为考虑到您的所有团队成员(如果还有其他成员)可能都会去在.gitignore
中列出相同的文件。You can add
.gitignore
to your.gitignore
file, but you probably just want to commit it anyway considering all of your team members (if there are any others) are probably going to have the same files listed in.gitignore
.为了澄清其他人所说的内容,大致有三类被排除的东西,并且 git 对每个类别使用不同的排除文件:
参与该项目的每个人都想要排除的东西 - 例如
* .o
、生成的可执行文件的名称、您不想签入的其他构建生成的文件。对于这种情况,您在项目目录中使用
.gitignore
(正如您所做的那样)。由于您希望参与该项目的每个人都共享这些排除项,因此您应该使用git add
添加这些.gitignore
文件,并像源文件一样提交它们。您个人希望在所有项目中排除的内容,但从事同一项目的其他人可能不会。例如,我使用 Emacs 编辑器,它会创建像
Foo.~1~
这样的备份文件,因此我个人在所有 git checkout 中都排除了这些文件对于这种情况,您使用“个人”忽略文件,通常位于您的主目录中,名为
~/.gitignore
;该文件的名称实际上设置为全局 git 参数,因此,例如,您可以这样做:[由于这是全局设置,因此您只需执行一次。]
因此,对于我的 Emacs 备份文件,我将
*~
之类的模式添加到~/.gitignore
。您希望在特定工作树中“本地”排除的内容,但可能不适用于其他工作树。
对于这种情况,您应该将排除条目添加到
.git/info/exclude
文件中。由于此文件位于神奇的.git
目录下,因此 status 命令永远不会将其显示为未跟踪的文件(并且您无法添加它)。Git 使用所有这些排除文件来决定排除哪些内容。
To clarify a bit on what others have said, there are rougly three categories of stuff that are excluded, and git uses different exclude files for each category:
Stuff that everybody working on the project wants to exclude — e.g.
*.o
, the names of the generated executables, other build-generated files that you don't want to check in.For this case, you use
.gitignore
in the project directories (as you're doing). Since you want everybody working on the project to share these excludes, you should add the these.gitignore
files usinggit add
and commit them like source files.Stuff that you personally want to exclude for all your projects, but others working on the same projects may not. For instance, I use the Emacs editor, which creates backup files like
Foo.~1~
, so that's something I exclude personally for all my git checkoutsFor this case, you use a "personal" ignore file, conventionally in your home directory called something like
~/.gitignore
; the name of this file is actually set as a global git parameter, so for instance, you might do:[As that's a global setting, you only need to do it once.]
So for my Emacs backup files, I add a pattern like
*~
to~/.gitignore
.Stuff that you want excluded "locally" in a specific working-tree, but maybe not for other working-trees.
For this case, you should add exclude entries to the
.git/info/exclude
file. Since this file is underneath the magic.git
directory, the status command will never show it as an untracked file (and you can't add it).Git uses all these exclude files together to decide what's excluded.
只需提交文件即可。
然后它将从列表中消失。
(我最初发现这令人困惑。)
您将来仍然可以对其进行编辑以添加更多项目。
然后再次承诺忽略它们
Just commit the file.
It will then go away from the list.
(I found this confusing initially.)
You'll still be able to edit it to add more items in the future.
And then commit again to ignore them
.gitignore
应该是您代码的一部分。您肯定不想排除,因为这是告诉您的队友(或另一台电脑上的您自己)不应提交哪些文件的方式.gitignore
should be a part of your code. You sure don't want to exclude since it's the way to tell your teammates (or yourself in another pc) which files shouldn't be commited