可以直接在Git子模块中开发吗?

发布于 2024-10-17 12:41:14 字数 319 浏览 2 评论 0原文

我有两个项目(A 和 B)。他们都使用 Common 项目。我想将 Common 包含到 A & 中B 通过子模块,因为这样我就可以直接将 A 和 B 中的每个提交绑定在一起。 B 他们共同依赖的承诺。

过去我试图让我的团队使用这样的子模块,但我们无法让它顺利工作。我们正在从子模块本身内部开发通用代码&从子模块提交,但我们遇到了很多问题,因此我们恢复将所有项目都放在同一目录下(C:\dev\A、C:\dev\Common)。

我很确定我们不知道子模块应该如何使用,但是如果你不能直接在子模块中开发公共代码,那不是会让开发变得更加困难吗?有人可以解释一下子模块的正确用法吗?

I have two projects (A & B). They both use project Common. I want to include Common into A & B via submodules because then I can directly tie each commit in A & B to which commit they rely on in Common.

In the past I tried to get my team using submodules like this, but we couldn't get it to work smoothly. We were developing Common code from within the submodule itself & committing from the submodule but we ran into so many problems that we reverted to having all projects under the same directory (C:\dev\A, C:\dev\Common).

I'm pretty sure we have no idea how submodules are supposed to be used, but if you can't develop Common code directly in the submodule, doesn't that make it more difficult to develop? Can someone please explain the proper usage of submodules?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期以后 2024-10-24 12:41:14

您可以直接从子模块进行开发(正如我在“子模块的真实性质”中解释的那样) ).
但是每次您在子模块中提交某些内容(并将其发布到某个地方)时,您都需要确保也在父存储库中提交。

现在您有两个子模块组织:

  • 递归包含
A
  Common
    (Common could depend itself on other dependencies)
B
  Common
    ...
  • 直接包含(仅直接依赖项)
ParentA
   A
   Common
   Other A dependencies
ParentB
   B
   Common
   Other B dependencies

(实际上,如果Common有自己的依赖项,< code>A 或 B 将依赖于“ParentCommon”,它将引用 Common 及其所有直接依赖项)

除非您有一个结构命令,其中 Common 必须A 的子目录,我会推荐第二个组织,它更接近于您的 C: \dev\AC:\dev\Common 一个。

实际上,我只喜欢一个深度依赖项,对于 ParentA,我将所有依赖项(直接和间接)列为子模块。

这是因为,对于任何尝试管理依赖项的工具,您仍然需要做出以下决策:

  • 依赖项冲突(A 依赖于 X,它需要 Z1 和需要 Z2Y:您想在项目中使用/编译哪个版本的 Z?)
  • 依赖项覆盖(当A 依赖于 X,它需要 Z1,而 A 也选择使用 X2 )
  • 依赖关系循环(其中 A 依赖于 B,而 B 又依赖于 C,而 C 使用...A!)

You can develop directly from a submodule (as I explained in "true nature of submodules").
But each time you commit something in the submodule (and publish it somewhere), you need to make sure to commit as well in the parent repository.

Now you have two submodules organizations:

  • recursive includes
A
  Common
    (Common could depend itself on other dependencies)
B
  Common
    ...
  • or direct includes (direct dependencies only)
ParentA
   A
   Common
   Other A dependencies
ParentB
   B
   Common
   Other B dependencies

(Actually, if Common had dependencies of its own, A or B would depend on "ParentCommon" which would refer to Common and all its direct dependencies)

Unless you have a structural imperative where Common must be a subdirectory of A, I would recommend the second organization, which is closer to your C:\dev\A, C:\dev\Common one.

Actually, I prefer only one depth dependencies, where for ParentA, I list all dependencies, direct and indirect, as submodules.

That is because, with any tool attempting to manage dependencies, you will still need to make decisions on:

  • dependencies conflicts (A depends on X which needs Z1, and Y which needs Z2: which version of Z do you want to use/compile in your project?)
  • dependencies override (when A depends on X which needs Z1, whereas A also choose to use X2)
  • dependencies cycle (where A depends on B which relies on C which uses... A!)
偏爱自由 2024-10-24 12:41:14

子模块并不是像您在这里所希望的那样工作得特别顺利。您可能已经注意到,要在子模块中提交对项目的更改(从 Git v1.7 开始,可能是永久的),您需要:

  1. 在子模块中进行更改。
  2. 提交到子模块,获取新的 SHA 哈希值。
  3. 使用新哈希更新外部项目的 .gitmodules 文件。
  4. 致力于外部项目。

如果您要同步开发子模块和外部项目,这会很麻烦。就混合代码库时更喜欢稳定的软件配置而不是简单性而言,这是“正确的方法”,但在这种情况下命令序列异常长。

然而,弹出原因堆栈,使用子模块进行同步开发可能表明了几个更大的问题之一:

  • 您的子组件之间没有清晰的接口,并且实现细节会跨越边界泄漏。
  • 您的子组件没有经过深思熟虑的接口,因此在解决更有趣的问题时,您必须不断地重新发明东西。
  • 您的界面稳定且高质量,但您对子模块代码没有良好的质量检查流程,因此您在尝试完成其他工作的同时不断修复该问题。
  • (特别是如果A和B不断改变不同的东西)你的“公共”代码实际上比你想象的要小得多,或者应该以不同的方式划分。

这些情况都不一定适合您,但这些问题是导致您遇到可怕的锁步子模块更新工作流程的问题之一。当您几乎可以依赖共享库和头文件时,子模块工作得很好,但是有一些令人信服的原因(例如,必须一致的奇怪的编译标志)需要从源代码进行编译。我的方法是解决这些问题(如果它们是真正的根本原因)。

但是,如果不存在这些问题,可能是 git 不适合您。说起来我很痛苦,但这绝对是一种可能性。

Submodules are not something that works particularly smoothly, the way you intend it here. As you've probably noticed, to commit a change to a project in a submodule (as of Git v1.7, probably in perpetuity), you need to:

  1. Make the change in the submodule.
  2. Commit to the submodule, getting a new SHA hash.
  3. Update the outer project's .gitmodules file with the new hash.
  4. Commit to the outer project.

This is cumbersome if you're developing the submodule and the outer project(s) in lockstep. It's "the right way," as far as preferring stable software configurations over simplicity when mixing codebases, but the command sequence is pathologically long in this case.

Popping the why stack, though, lockstep development with submodules probably indicates one of several larger problems:

  • You do not have a clear interface between your subcomponent, and implementation details leak across the boundaries.
  • You do not have a well-thought interface to your subcomponent, so you keep having to reinvent things as you solve a more interesting problem.
  • Your interface is stable and high-quality, but you do not have good QA process on the submodule code, so instead you're constantly fixing that while trying to get other work done.
  • (Esp. if A and B keep changing different things) Your "common" code is actually much smaller than you think, or should be divided differently.

None of these cases are necessarily true of you, but these are among the problems that cause the horrible lockstep submodule update workflow you're having. Submodules work pretty well when you could almost rely on shared libraries and header files, but there's some compelling reason (e.g. weird compile flags that must be consistent) to compile from source. My approach would be to fix these problems, if they're the real root causes.

If these problems aren't there, however, it may be that git is the wrong tool for you. It pains me to say it, but this is definitely a possibility.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文