在git中具有子模块的浅表克隆,如何使用指向的提交而不是最新的提交? [英] Shallow clone with submodules in git, how to use pointed commits and not latest ones?
问题描述
我知道使用--depth
选项和git submodule update
可能有浅层子模块.
I know it's possible to have shallow submodules using the --depth
option with git submodule update
.
但是,如果我跑步
git submodule update --init --recursive --depth 1
在具有几个子模块的项目中,我得到一个fatal: reference is not a tree
.所以我再次尝试了
on my project with several submodules, I get a fatal: reference is not a tree
. So I've tried again with
git submodule update --init --recursive --depth 100
它正常工作,但是我想将深度保持在1.
which worked correctly, however I'd like to keep depth at 1.
在我看来,--depth 1
提取子模块中的最新提交,而不是主存储库指向的提交,这就是为什么将其设置为100可以解决问题的原因,因为它提取了更长的提交列表,包括尖的.
It seems to me that --depth 1
pulls the latest commit in the submodule, not the commit pointed by the main repository, and that's why setting it to 100 solved the problem, since it pulled a longer list of commits, including the pointed ones.
不幸的是,如果是这种情况,那么我通常不能确定最近的100次提交中是否包含我需要的提交.
Unfortunately, if that's the case then I cannot generally be sure the latest 100 commits include the one I need.
我如何告诉git使用带尖头的提交,而不是浅克隆中带有子模块的最新提交?
How do I tell git to use the pointed commits and not the latest ones in the shallow clone with submodules?
我在Windows上使用git 1.9.4.
I'm using git 1.9.4 on Windows.
推荐答案
不幸的是,似乎没有办法仅使用特定的提交ID来执行git fetch
.如果有,我真的很想知道.但是,我用两种不同的方式处理您的问题:一种,如果可能的话,是在远程存储库上创建一个标记或分支,该标记或分支指向我想要的提交,例如我们称其为"ref".这样,我可以获取远程引用,并且--depth
将适用于它,而不是最新的技巧.我代替了git submodule update --init
:
Unfortunately there doesn't seem to be a way to do a git fetch
with only a specific commit ID. If there is I would really want to know. However I have dealt with your problem in two different ways: one, if it's possible, is to create a tag or branch on the remote repository which points to the commit I want, say we call it "ref". This way I can fetch the remote ref and the --depth
will apply to it, not the latest tip. Instead of git submodule update --init
I do:
git submodule init
git clone --depth 1 -b ref --separate-git-dir .git/modules/sub/ repository sub
其中"sub"是子模块的名称和路径(为简单起见,它们是相同的)
where "sub" is the name and path of the submodule (for simplicity let's say they are the same)
如果无法在远程存储库中创建标签或分支,则另一种方法是在循环中搜索所需的提交:
The other way, if you can't create tags or branches in the remote repository, is to search for the commit you want in a loop:
git submodule init
id=$(git submodule status|sed -ne 's/.\([a-z0-9]*\) sub.*/\1/p'
git clone --depth 1 --separate-git-dir -n .git/modules/sub/ repository sub
cd sub
while ! git rev-list $id ; do
git fetch --depth $((i+=1))
done
git checkout $id
您可以一次增加一个以上的提交,以使其更快,但最终可能会比您想要的提交更早一些.
You could increment with more than just one commit at a time to make it go faster, but you might end up with some earlier commits than the one you want.
这篇关于在git中具有子模块的浅表克隆,如何使用指向的提交而不是最新的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!