git 多个`.gitignore`s 不赞成吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3305869/
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
Are multiple `.gitignore`s frowned on?
提问by Conley Owens
Unless a repo consisted of several independent projects, it seems it would be simplest to just have one .gitignore
file at the root of the repo than various ones throughout. Is there a standard best practice on this or some analysis online of when one approach is better than the other?
除非一个 repo 由几个独立的项目组成,否则在 repo.gitignore
的根目录下只有一个文件比在整个项目中使用多个文件更简单。是否有标准的最佳实践或一些在线分析何时一种方法比另一种更好?
回答by Jakub Nar?bski
I can think of at least two situations where you would want to have multiple .gitignore
files in different (sub)directories.
我可以想到至少两种情况,您希望.gitignore
在不同的(子)目录中有多个文件。
Different directories have different types of file to ignore. For example the
.gitignore
in the top directory of your project ignores generated programs, whileDocumentation/.gitignore
ignores generated documentation.Ignore given files only in given (sub)directory (you can use
/sub/foo
in.gitignore
, though).
不同的目录有不同类型的文件要忽略。例如,
.gitignore
在您的项目的顶级目录中忽略生成的程序,而Documentation/.gitignore
忽略生成的文档。仅忽略给定(子)目录中的给定文件(不过,您可以使用
/sub/foo
in.gitignore
)。
Please remember that patterns in .gitignore
file apply recursively to the (sub)directory the file is in and all its subdirectories, unless pattern contains '/' (so e.g. pattern name
applies to any file named name
in given directory and all its subdirectories, while /name
applies to file with this name only in given directory).
请记住.gitignore
文件中的模式递归地应用于文件所在的(子)目录及其所有子目录,除非模式包含“/”(例如模式name
适用name
于给定目录及其所有子目录中命名的任何文件,而/name
适用于文件仅在给定目录中使用此名称)。
回答by Aristotle Pagaltzis
As a tangential note, one case where the ability to have multiple .gitignore
files is very useful is if you want an extra directory in your working copy that you never intend to commit. Just put a 1-byte .gitignore
(containing just a single asterisk) in that directory and it will never show up in git status
etc.
作为切入点,拥有多个.gitignore
文件的能力非常有用的一种情况是,如果您希望工作副本中有一个您永远不打算提交的额外目录。只需.gitignore
在该目录中放置一个 1 字节(仅包含一个星号),它就永远不会出现在git status
etc中。
回答by VonC
You can have multiple .gitignore
, each one of course in its own directory.
To check which gitignore rule is responsible for ignoring a file, use git check-ignore
: git check-ignore -v -- afile
.
您可以有多个.gitignore
,当然每个都在自己的目录中。
要检查哪个 gitignore 规则负责忽略文件,请使用git check-ignore
: git check-ignore -v -- afile
。
And you can have different version of a .gitignore
file per branch: I have already seen that kind of configuration for ensuring one branch ignores a file while the other branch does not: see this question for instance.
.gitignore
每个分支可以有不同版本的文件:我已经看到了确保一个分支忽略文件而另一个分支不忽略文件的那种配置:例如参见这个问题。
If your repo includes several independent projects, it would be best to reference them as submodulesthough.
That would be the actual best practices, allowing each of those projects to be cloned independently (with their respective .gitignore
files), while being referenced by a specific revision in a global parent project.
See true nature of submodulesfor more.
如果您的 repo 包含多个独立项目,最好将它们作为子模块引用。
这将是实际的最佳实践,允许独立克隆每个项目(使用它们各自的.gitignore
文件),同时由全局父项目中的特定修订版引用。
有关更多信息,请参阅子模块的真实性质。
Note that, since git 1.8.2 (March 2013) you can do a git check-ignore -v -- yourfile
in order to see which gitignore run (from which .gitignore
file) is applied to 'yourfile
', and better understand why said file is ignored.
See "which gitignore
rule is ignoring my file?"
请注意,从 git 1.8.2(2013 年 3 月)开始,您可以执行 agit check-ignore -v -- yourfile
以查看哪个 gitignore 运行(来自哪个.gitignore
文件)应用于“ yourfile
”,并更好地理解为什么忽略所述文件。
请参阅“哪个gitignore
规则忽略了我的文件?”
回答by Paul Draper
Pro single
亲单
Easy to find.
Hunting down exclusion rules can be quite difficult if I have multiple gitignore, at several levels in the repo.
With multiple files, you also typically wind up with a fair bit of duplication.
容易找到。
如果我在 repo 的多个级别有多个 gitignore,则查找排除规则可能会非常困难。
对于多个文件,您通常还会有相当多的重复。
Pro multiple
亲多
Scopes "knowledge" to the part of the file tree where it is needed.
Since Git only tracks files, an empty .gitignore is the only way to commit an "empty" directory.
(And before Git 1.8, the only way to exclude a pattern like
my/**.example
was to createmy/.gitignore
in with the pattern**.foo
. This reason doesn't apply now, as you can do/my/**/*.example
.)
将“知识”范围限定到需要它的文件树部分。
由于 Git 仅跟踪文件,因此空的 .gitignore 是提交“空”目录的唯一方法。
(和Git 1.8之前,只有这样,才能排除格式,如:
my/**.example
是建立my/.gitignore
在与图案**.foo
,这理由现在不适用,因为你可以做/my/**/*.example
。)
I much prefer a single file, where I can find all the exclusions. I've never missed per-directory .svn, and I won't miss per-directory .gitignore either.
我更喜欢单个文件,在那里我可以找到所有排除项。我从来没有错过每个目录的 .svn,我也不会错过每个目录的 .gitignore。
That said, multiple gitignores are quite common. If you do use them, at least be consistent in their use to make them reasonable to work with. For example, you may put them in directories only one level from the root.
也就是说,多个 gitignore 很常见。如果你确实使用它们,至少在它们的使用上保持一致,以使它们合理地使用。例如,您可以将它们放在距根目录仅一级的目录中。
回答by Lukman
There are many scenarios where you want to commit a directory to your Git repo but without the files in it, for example the logs
, cache
, uploads
directories etc.
在许多情况下,您希望将目录提交到 Git 存储库但其中没有文件,例如logs
、cache
、uploads
目录等。
So what I always do is to add a .gitignore
file in those directories with the following content:
所以我总是.gitignore
在这些目录中添加一个文件,内容如下:
*
!.gitignore
With this .gitignore
file, Git will not track any files in those directories yet still allow me to add the .gitignore
file and hence the directory itself to the repo.
有了这个.gitignore
文件,Git 将不会跟踪这些目录中的任何文件,但仍然允许我将.gitignore
文件和目录本身添加到 repo。