为什么“git submodule”每次都需要从远程存储库获取?
仅从 SO 上“git 子模块”的结果数量来看,这显然是一个常见且容易混淆的主题,因此我将尽力尽可能准确。
忘记关于更新/提交/分支子模块的一切(我知道这会让事情变得非常复杂),为什么每次我更改分支时子模块都会被清空?根据我目前的理解,这使得分支机构变得昂贵;如果我在机场并且无法轻松/廉价地转接怎么办?是我做错了什么,还是有一些我还没有意识到的开发理念?
例子永远不会造成伤害:
## make a new project
$> git --version
git version 1.7.5.4
$> mkdir new_proj; cd new_proj; git init
$> touch new_file_1.txt; touch new_file_2.txt
$> git add . && git commit -m "first commit"
## move into some development branch
$> git checkout -b cool_feature
$> <hack hack hack>
# in the middle, I add a submodule
$> git submodule add https://github.com/some/other_proj.git other_proj
$> git submodule update --init
$> ls -lR
new_file_1.txt
new_file_2.txt
other_proj
other_proj/that_file
other_proj/another_file
## I have to go back to master to do some work
$> git checkout master
# Why is other_proj still around?
$> git status
Untracked: other_proj
## Fine, I'll remove it, since I want a clean working copy, because I need to do some work and commits
$> git clean -f -d
$> <work work work>
## Now I'm ready to go back to cool_feature, but my submodules are empty!
$> git checkout cool_feature
此时,我应该git submodule update
,但是如果我不能/它很昂贵怎么办(例如它是远程的,而且我没有互联网访问/它很慢) )。
我想出的最好的解决方法是将我关心的所有子模块克隆到一个完全独立的位置,然后从我的本地克隆中克隆子模块;这保留了子模块的廉价性。当然,当您在团队中工作时,这会增加另一层复杂性。 :/
Judging just from the number of results for "git submodule" here on SO alone, this is clearly a commonly asked and easily confused topic, so I will try to be as precise as possible.
Forgetting everything about updating/committing/branching submodules (which I understand greatly complicates things), why do submodules get emptied each time I change branches? From my current understanding, this makes branches expensive; what if I'm at the airport, and can't easily/cheaply connect? Am I doing something wrong, or there's some development philosophy that I'm not yet aware of?
Examples never hurt:
## make a new project
gt; git --version
git version 1.7.5.4
gt; mkdir new_proj; cd new_proj; git init
gt; touch new_file_1.txt; touch new_file_2.txt
gt; git add . && git commit -m "first commit"
## move into some development branch
gt; git checkout -b cool_feature
gt; <hack hack hack>
# in the middle, I add a submodule
gt; git submodule add https://github.com/some/other_proj.git other_proj
gt; git submodule update --init
gt; ls -lR
new_file_1.txt
new_file_2.txt
other_proj
other_proj/that_file
other_proj/another_file
## I have to go back to master to do some work
gt; git checkout master
# Why is other_proj still around?
gt; git status
Untracked: other_proj
## Fine, I'll remove it, since I want a clean working copy, because I need to do some work and commits
gt; git clean -f -d
gt; <work work work>
## Now I'm ready to go back to cool_feature, but my submodules are empty!
gt; git checkout cool_feature
At this point, I'm supposed to git submodule update
, but what if I can't/it's expensive (e.g. it's remote, and I don't have internet access/it's slow).
The best workaround I've come up with is to clone all the submodules that I care about into a completely separate location, and then submodule from my local clones; this preserves the cheapness of submodules. Of course, this adds another layer of complexity when you're working on a team. :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑子模块只不过是指向另一个存储库的提交的指针< /a>,git子模块更新有点不可避免(为了取回与所述指针关联的内容)。
另一种解决方法是克隆您的主存储库:
从一个分支切换到另一个分支不需要
git checkout
(及其关联的git submodule update
),但是路径发生了变化。如果您想在一个目录中工作,另一种解决方法已在“用 git 子模块替换了第三方代码,现在我无法切换分支"
Considering a submodule is nothing more than a pointer to a commit of another repo, the
git submodule update
is a bit unavoidable (in order to get back the content associated with said pointer).One other workaround would be to clone your main repo:
Switching from one branch to another wouldn't require a
git checkout
(and its associatedgit submodule update
), but a change of path.The other workaround, if you want to work in one directory, has been described in "Replaced third party code with git submodules, now I can't switch branches"