您应该将 .gitignore 提交到 Git 存储库中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5765645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:44:11  来源:igfitidea点击:

Should you commit .gitignore into the Git repos?

gitgitignore

提问by Howard

Do you think it is a good practice to commit .gitignore into a Git repo?

您认为将 .gitignore 提交到 Git 存储库是一个好习惯吗?

Some people don't like it, but I think it is good as you can track the file's history. Isn't it?

有些人不喜欢它,但我认为它很好,因为您可以跟踪文件的历史记录。不是吗?

采纳答案by Bruce Stephens

Normally yes, .gitignoreis useful for everyone who wants to work with the repository. On occasion you'll want to ignore more private things (maybe you often create LOGor something. In those cases you probably don't want to force that on anyone else.

通常是的,.gitignore对于想要使用存储库的每个人都很有用。有时你会想要忽略更多私人的东西(也许你经常创建LOG或其他东西。在这些情况下,你可能不想把它强加给其他人。

回答by Stéphan Kochen

You typically docommit .gitignore. In fact, I personally go as far as making sure my index is always clean when I'm not working on something. (git statusshould show nothing.)

您通常提交.gitignore。事实上,当我不工作时,我个人会确保我的索引总是干净的。(git status应该什么都不显示。)

There are cases where you want to ignore stuff that really isn't project specific. For example, your text editor may create automatic *~backup files, or another example would be the .DS_Storefiles created by OS X.

在某些情况下,您想忽略真正不是项目特定的内容。例如,您的文本编辑器可能会创建自动*~备份文件,或者另一个示例是.DS_StoreOS X 创建的文件。

I'd say, if others are complaining about those rules cluttering up your .gitignore, leave them out and instead put them in a global excludes file.

我想说的是,如果其他人抱怨这些规则使您的.gitignore.

By default this file resides in $XDG_CONFIG_HOME/git/ignore(defaults to ~/.config/git/ignore), but this location can be changed by setting the core.excludesfileoption. For example:

默认情况下,此文件驻留在$XDG_CONFIG_HOME/git/ignore(默认为~/.config/git/ignore)中,但可以通过设置该core.excludesfile选项来更改此位置。例如:

git config --global core.excludesfile ~/.gitignore

Simply create and edit the global excludesfile to your heart's content; it'll apply to every git repository you work on on that machine.

只需创建和编辑全局排除文件即可满足您的需求;它将适用于您在该机器上工作的每个 git 存储库。

回答by Bitdiot

I put commit .gitignore, which is a courtesy to other who may build my project that the following files are derived and should be ignored.

我提交了 .gitignore,这是对其他可能构建我的项目的人的礼貌,因为以下文件是派生的,应该被忽略。

I usually do a hybrid. I like to make makefile generate the .gitignore file since the makefile will know all the files associated with the project -derived or otherwise. Then have a top level project .gitignore that you check in, which would ignore the generated .gitignore files created by the makefile for the various sub directories.

我通常做混合动力车。我喜欢让 makefile 生成 .gitignore 文件,因为 makefile 会知道与项目相关的所有文件 - 派生或其他。然后有一个您签入的顶级项目 .gitignore,它将忽略由 makefile 为各种子目录创建的生成的 .gitignore 文件。

So in my project, I might have a bin sub directory with all the built executables. Then, I'll have my makefile generate a .gitignore for that bin directory. And in the top directory .gitignore that lists bin/.gitignore. The top one is the one I check in.

所以在我的项目中,我可能有一个包含所有构建的可执行文件的 bin 子目录。然后,我会让我的 makefile 为那个 bin 目录生成一个 .gitignore。并在列出 bin/.gitignore 的顶级目录 .gitignore 中。最上面的就是我签到的。

回答by user3464496

Committing .gitignore can be very useful but you want to make sure you don't modify it too much thereafter especially if you regularly switch between branches. If you do you might get cases where files are ignored in a branch and not in the other, forcing you to go manually delete or rename files in your work directory because a checkout failed as it would overwrite a non-tracked file.

提交 .gitignore 可能非常有用,但您要确保此后不会对其进行过多修改,尤其是如果您经常在分支之间切换。如果这样做,您可能会遇到在一个分支中忽略文件而不在另一个分支中的情况,迫使您手动删除或重命名工作目录中的文件,因为签出失败,因为它会覆盖未跟踪的文件。

Therefore yes, do commit your .gitignore, but not before you are reasonably sure it won't change that much thereafter.

因此,是的,请提交您的 .gitignore,但不要在您合理确定此后它不会发生太大变化之前提交。

回答by Jakub Nar?bski

It is a good practice to .gitignoreat leastyour build products (programs, *.o, etc.).

.gitignore至少构建产品(程序、*.o 等)是一种很好的做法。