如何设置 git 项目以使用外部 repo 子模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2140985/
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
How to set up a git project to use an external repo submodule?
提问by Tom
I'd like to create a repo which pulls in a remote repo.
我想创建一个包含远程存储库的存储库。
For example, let's say jQuery as a submodule:
例如,假设 jQuery 作为一个子模块:
git://github.com/jquery/jquery.git
What would be the process of creating a repo with jQuery as a submodule and adding my own external as a remote repo.
使用 jQuery 作为子模块创建一个 repo 并将我自己的外部添加为远程 repo 的过程是什么。
Also once this is setup, if I push / pull to my own remote, will the external remain intact?
此外,一旦设置完成,如果我推/拉到我自己的遥控器,外部是否会保持完整?
回答by memmons
- You have a project -- call it MyWebAppthat already has a github repo
- You want to use the jquery repositoryin your project
- You want to pull the jquery repo into your project as a submodule.
- 你有一个项目——称之为MyWebApp,它已经有一个 github 仓库
- 您想在您的项目中使用jquery 存储库
- 您想将 jquery 存储库作为子模块拉入您的项目中。
Submodules are really, really easy to reference and use. Assuming you already have MyWebApp set up as a repo, from terminal issue these commands:
子模块真的非常容易参考和使用。假设您已经将 MyWebApp 设置为存储库,从终端发出以下命令:
cd MyWebApp
git submodule add git://github.com/jquery/jquery.git externals/jquery
This will create a directory named externals/jquery
* and link it to the github jquery repository. Now we just need to init the submodule and clone the code to it:
这将创建一个名为externals/jquery
*的目录并将其链接到 github jquery 存储库。现在我们只需要初始化子模块并将代码克隆到它:
git submodule update --init --recursive
You should now have all the latest code cloned into the submodule. If the jquery repo changes and you want to pull the latest code down, just issue the submodule update
command again. Please note: I typically have a number of external repositories in my projects, so I always group the repos under an "externals" directory.
您现在应该将所有最新代码克隆到子模块中。如果 jquery repo 更改并且您想拉下最新代码,只需submodule update
再次发出命令。请注意:我的项目中通常有许多外部存储库,因此我总是将这些存储库分组在“externals”目录下。
The online Pro Git Bookhas some good information on submodules (and git in general) presented in an easy-to-read fashion. Alternately, git help submodule
will also give good information. Or take a look at the Git Submodule Tutorialon the git wiki.
在线Pro Git Book以易于阅读的方式提供了一些关于子模块(以及一般的 git)的很好的信息。或者,git help submodule
也会给出很好的信息。或者查看git wiki 上的Git 子模块教程。
I noticed this blog entry which talks about submodules and compares them to Subversion's svn:externals mechanism: http://speirs.org/blog/2009/5/11/understanding-git-submodules.html
我注意到这个博客条目谈论子模块并将它们与 Subversion 的 svn:externals 机制进行比较:http: //speirs.org/blog/2009/5/11/understanding-git-submodules.html
* As a best practice, you should always place your submodules in their own directory, such as Externals. If you don't, your root project directory can become very cluttered very fast.
* 作为最佳实践,您应该始终将子模块放在它们自己的目录中,例如 Externals。如果你不这样做,你的根项目目录会很快变得非常混乱。
回答by WhyNotHugo
Most of what you need to know has already been answered, so I won't bother addressing that, however, I've found a small piece of information that's usually missing.
您需要知道的大部分内容已经得到解答,因此我不会费心解决这个问题,但是,我发现了一小部分通常会丢失的信息。
As you know, "git pull" won't update the submodules, and "git submodules update" won't download the latest HEAD of those submodules either.
如您所知,“git pull”不会更新子模块,“git submodules update”也不会下载这些子模块的最新 HEAD。
To update all of your submodules to their latest upstream revision, you can use
要将所有子模块更新到最新的上游修订版,您可以使用
git submodule foreach git pull
If you often alter your submodules, and have lots of the, then "git foreach" will become invaluable.
如果你经常改变你的子模块,并且有很多,那么“git foreach”将变得非常宝贵。
回答by Tom
In the end I found http://github.com/evilchelu/braidit seemed to fit with how I expected submodules and remotes to work
最后我发现http://github.com/evilchelu/braid它似乎符合我期望子模块和遥控器的工作方式
回答by Dapaldo
I think that the @Hugo answer could be what you need and works fine. So I have found a easier way.
我认为@Hugo 的答案可能是您所需要的并且工作正常。所以我找到了一个更简单的方法。
git submodule update --remote
That's all.
就这样。
So a complete workflow could be:
所以一个完整的工作流程可能是:
git clone project-with-submodules
git submodule init
git config -l
git submodule update --remote