如何检查具有指定分支和稀疏结账的巨大git仓库中的子目录?

时间:2016-03-05 22:16:34

标签: git bash branch

例如,我想要获取此文件夹https://github.com/python/cpython/tree/2.7/Tools/freeze

我跑的命令是:

mkdir python
cd python
git init
git remote add origin https://github.com/python/cpython.git
git config core.sparsecheckout true
echo "Tools/freeze/*" >> .git/info/sparse-checkout

# find remote branches
git remote show origin

# this works and pulls only that folder
git pull --depth=1 origin master

# but this doesn't, why?
git pull --depth=1 origin 2.7

# but how do I switch to remote 2.7 branch?
git checkout --track -b 2.7 origin/2.7
fatal: Cannot update paths and switch to branch '2.7' at the same time.
Did you intend to checkout 'origin/2.7' which can not be resolved as commit?

我在某个地方读到了我需要在结账前运行git fetch,但这有点打乱稀疏结账的目的,我的互联网很慢而且回购很大。我怎样才能用分支2.7获取该子目录?谢谢!

这是在windows8和git bash

编辑: 如果我跑了 git pull --depth=1 origin 2.7它将拉远程2.7分支,但它也将所有其他文件带入我的工作目录,而如果我运行git pull --depth=1 origin master,它只会在主分支中带来Tools/freeze目录?为什么会这样?

另一个例子:

mkdir qt
cd qt
git init
git remote add origin https://github.com/qtproject/qt.git
git config core.sparsecheckout true
echo util/qlalr/examples/lambda/* >> .git/info/sparse-checkout
git pull --depth=1 origin 4.8

该文件夹util/qlalr/examples/lambda非常小,但是当它运行最后一个命令时,它仍然很慢,可以避免吗?

edit2:我意识到目前的git是不可能的。但我现在唯一的问题是为什么git pull --depth=1 origin 2.7不尊重稀疏结账配置?

4 个答案:

答案 0 :(得分:1)

您的结帐失败,因为拉(并因此获取)显式引用只提取 该引用,因此在您最初提取后,您的回购只有refs/heads/masterrefs/remotes/origin/master指着同一个提交。 2.7的结帐不起作用,因为您的回购没有任何名称。

Pull执行合并,并且在您的工作树中放置的额外内容git pull origin 2.7用于解决冲突,合并无法确定正确的结果,因此您必须这样做。您将看到并非检查Tools目录之外的所有内容,只检查冲突的文件。我不确定如何与浅层提取和稀疏检查合并应该是整体行为,但要求解决冲突肯定是唯一要做的事情。

执行浅的one-ref fetch与git一样轻量级,如果一次性使用带宽,那么你可以克隆到ec2实例并标记一个特定的树。

答案 1 :(得分:0)

试试这个

mkdir 
cd 
git init
git remote add -f origin <url>

这会创建一个空的存储库并提取所有对象,但不会检出它们。然后做:

git config core.sparseCheckout true

现在定义您想要的文件夹。这是完成添加。 GIT中/信息/稀疏结账,

回声&#34; some / dir /&#34; &GT;&GT; .git / info / sparse-checkout echo&#34;另一个/ sub / tree&#34; &GT;&GT;的.git /信息/稀疏结账

然后

git pull origin master

答案 2 :(得分:0)

首先设置config参数:

# Enable sparse-checkout:
git config core.sparsecheckout true

在.git / info / sparse-checkout中配置稀疏结账路径:

# Add the relevant path to the sparse-checkout file
echo cpython/tree/2.7/Tools/freeze >> .git/info/sparse-checkout

更新您的工作树:

git read-tree -mu HEAD
  

<强> git-read-tree
  将树信息读入索引

     

<强> -m
  执行合并,而不仅仅是阅读

     

<强> -u
  成功合并后,使用合并结果更新工作树中的文件。

sparse checkout

使用稀疏结账时,您基本上会告诉Git从工作树中排除某组文件。 这些文件仍然是存储库的一部分,但它们不会显示在您的工作目录中。

在内部,稀疏结帐使用 skip-worktree 标记将所有排除的文件标记为始终更新。

 
# enable sparse checkout in an existing repository:
git config core.sparseCheckout true

# Create a .git/info/sparse-checkout file containing the
# paths to include/exclude from your working directory. 

# Update your working directory with 
git read-tree -mu HEAD

enter image description here

答案 3 :(得分:0)

您必须创建一个本地分支以供参考。更新的步骤应为:

Host thresholds exceeded: [Threads]