在 Visual Studio 中使用 git 子模块的工作流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26951252/
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-19 10:34:12  来源:igfitidea点击:

Workflow for using git submodules in Visual Studio

gitvisual-studiovisual-studio-2013git-submodules

提问by h bob

I have some shared code I want to share among a number of solutions.Most examples use the command line, but I want to do it using Visual Studio 2013 (and/or TortoiseGit)?

我有一些共享代码,我想在许多解决方案中共享。大多数示例使用命令行,但我想使用 Visual Studio 2013(和/或 TortoiseGit)来完成?

- SolutionShared
  - .git
  - Project1Shared
  - Project2Shared
- Solution1
  - .git
  - ProjectFoo
  - ProjectBar
  - [SolutionShared]
    - [Project1Shared]
    - [Project2Shared]
- Solution2
  - .git
  - ProjectBaz
  - ProjectQux
  - [SolutionShared]
    - [Project1Shared]
    - [Project2Shared]

What I did was to create a new solution SolutionShared, add all my shared code there, and add it to its own git repo. I then used TortoiseGit (as I couldn't figure out how to do it Visual Studio) to add that shared repo as a git submodule to Solution1and Solution2.

我所做的是创建一个新的解决方案SolutionShared,在那里添加我所有的共享代码,并将其添加到自己的 git 存储库中。然后我使用 TortoiseGit(因为我不知道如何在 Visual Studio 中实现)将该共享存储库作为 git 子模块添加到Solution1Solution2.

1. What do I do in Visual Studio?
My two solutions now have a SolutionShareddirectory. Do I simply add its two child projects (Project1Sharedand Project2Shared) in Visual Studio?

1. 我在 Visual Studio 中做什么?
我的两个解决方案现在有一个SolutionShared目录。我是否只是在 Visual Studio 中添加它的两个子项目(Project1SharedProject2Shared)?

2. How do I make changes to the shared code from within the non-shared projects
If I'm in one of the non-shared solutions and make a change to something in the submodule, how do I commit and push it back to the shared solution's repo (SolutionShared) so that it's available to all solutions that reference it?

2. 如何从非共享项目中更改共享代码
如果我在非共享解决方案之一中并对子模块中的某些内容进行更改,我如何提交并将其推送回共享解决方案的 repo ( SolutionShared) 以便所有引用它的解决方案都可以使用它?

采纳答案by h bob

After much experimenting...

经过多次尝试...

In VS, add the shared projects from the submodule to the solution. They seem to live "outside" the parent solution as far as versioning.

在 VS 中,将子模块中的共享项目添加到解决方案中。就版本控制而言,它们似乎生活在父解决方案的“外部”。

If you make an edit to the submodule's projects, they are local. They need to be committed and pushed to the source repo, and then you need to merge them there. If you make changes at the source, you need to pull them manually into your solution's git submodule.

如果您对子模块的项目进行编辑,则它们是本地的。它们需要提交并推送到源代码库,然后您需要在那里合并它们。如果在源代码处进行更改,则需要手动将它们拉入解决方案的 git 子模块中。

Problem is VS doesn't do any of this for you, so you need to use something like TortoiseGit or the command line.

问题是 VS 不会为你做任何这些,所以你需要使用 TortoiseGit 或命令行之类的东西。

回答by CRender

We have found that the easiest way for us to do this is to move each of our shared units of code into their own Visual Studio Project and put each shared Visual Studio project in its own repo.

我们发现,最简单的方法是将我们的每个共享代码单元移动到他们自己的 Visual Studio 项目中,并将每个共享的 Visual Studio 项目放在自己的存储库中。

We then add each one of those projects as a submodule to any Solution that needs them. This is helpful since many of our projects can be very large in size and share many of the same chunks of code. We have made extensive use of nuget packages for this purpose, as well, but have generally had better success and a much better design/debug experience with submodules.

然后,我们将这些项目中的每一个作为子模块添加到需要它们的任何解决方案中。这很有帮助,因为我们的许多项目可能非常大,并且共享许多相同的代码块。为此,我们也广泛使用了 nuget 包,但通常取得了更好的成功和更好的子模块设计/调试体验。

Much has changed at Microsoft in relation to Git in the last few years. Visual Studio Team Services (TFS Online) and On Prem-TFS (since TFS2015) both now have a good understanding of how submodules work and can now do CI Builds that incorporate submodules right out of the box.

在过去几年中,与 Git 相关的 Microsoft 发生了很大变化。Visual Studio Team Services(TFS Online)和 On Prem-TFS(自 TFS2015 起)现在都对子模块的工作原理有了很好的理解,并且现在可以执行包含开箱即用的子模块的 CI 构建。

Support in on-prem TFS 2015 can be a bit buggy, however. TFS Build's references to the submodules have a habit of becoming corrupted resulting in builds that stop working without warning until whichever submodule is at fault is completely removed and re-added - not a fun process. For this reason (among a few others), we are now using VSTS (TFS Online) for everything and have had none of those same types of problems. I would assume that this is fixed in on-prem TFS 2017, as well.

但是,对本地 TFS 2015 的支持可能有点问题。TFS Build 对子模块的引用有一种被破坏的习惯,导致构建在没有警告的情况下停止工作,直到完全删除并重新添加出现故障的子模块 - 这不是一个有趣的过程。出于这个原因(以及其他一些原因),我们现在对所有内容都使用 VSTS(TFS Online),并且没有遇到任何相同类型的问题。我认为这在本地 TFS 2017 中也已修复。

As was mentioned, Visual Studio itself (the IDE) does still have a hard time understanding submodule relationships. It really just can't handle them. I have tried a number of standalone Git Tools to find the easiest way to manage an environment like this. Tortoise seems to provide the easiest and most performant experience when pushing, pulling, and checking in to repos containing submodules. I usually use commands to add the submodules, but I suspect Tortoise's add submodule functionality works just fine, too.

如前所述,Visual Studio 本身(IDE)仍然很难理解子模块关系。实在是受不了他们。我尝试了许多独立的 Git 工具来找到管理这样的环境的最简单方法。在推、拉和检入包含子模块的存储库时,Tortoise 似乎提供了最简单和最高效的体验。我通常使用命令来添加子模块,但我怀疑 Tortoise 的添加子模块功能也能正常工作。

When you commit code to a repo using Tortoise, it notices when a submodule is dirty and will prompt you to check-in the submodule before checking in the parent repo. That's really nice. Pulling or Fetching the parent repo can be a little confusing, though. It doesn't actually refresh the submodule from it's remote branch, it only refreshes it to whatever level is currently checked into the main repo remote which is not always the latest. In reality, this is the desired behavior, it's just not immediately intuitive if you're not expecting it.

当您使用 Tortoise 将代码提交到存储库时,它会注意到子模块何时变脏并提示您在检入父存储库之前检入子模块。这是非常好的。不过,拉取或获取父存储库可能有点令人困惑。它实际上并没有从它的远程分支刷新子模块,它只是将它刷新到当前签入主 repo 远程的任何级别,这并不总是最新的。实际上,这是所需的行为,只是如果您不期望它就不会立即直观。