如何使用指定的分支和稀疏签出在大型git repo中签出子目录?

如何使用指定的分支和稀疏签出在大型git repo中签出子目录?,git,bash,branch,Git,Bash,Branch,例如,我想得到这个文件夹 我运行的命令是: 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 ori

例如,我想得到这个文件夹

我运行的命令是:

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
不尊重稀疏签出配置

试试这个

mkdir 
cd 
git init
git remote add -f origin <url>
现在定义所需的文件夹。将其添加到中即可完成此操作。 git/info/sparse签出

echo“some/dir/”>.git/info/sparse签出echo“other/sub/tree”>.git/info/sparse签出

然后


首先设置配置参数:

# 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读取树

将树信息读入索引

-m

执行合并,而不仅仅是读取

-u

成功合并后,使用合并结果更新工作树中的文件


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

在内部,稀疏签出使用
跳过工作树
标志将所有排除的文件标记为始终更新


您的签出失败,因为拉取(并因此拉取)一个显式ref只获取该ref,因此在初始拉取之后,您的回购只有
refs/heads/master
refs/remotes/origin/master
,两者都指向同一提交。签出2.7不起作用,因为您的回购协议中没有该名称的任何内容

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


如果一次性带宽使用真的非常昂贵,那么执行浅层的单参考获取和git一样轻量级,您可以克隆到ec2实例并标记特定的树。

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

git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "finisht/*" >> .git/info/sparse-checkout
git branch -b <your branch>
git pull --depth=1 origin <your branch>
git init
光盘
git远程添加源
git config core.sparsecheckout true
echo“finisht/*”>.git/info/sparse checkout
吉特分行-b
git pull--深度=1原点

谢谢,但整个想法是不要使用-f“fetch”?因为在我的低速互联网上获取速度非常慢。它仍然得到我不需要的所有数据,对吗?你是说我应该运行这个?只是试着不工作。你能下达全部命令吗?我明白你的意思了,你一定是从这里抄袭过来的:但最初的想法是不要克隆,我已经提到我希望避免克隆或获取整个回购。你可以在大型回购上尝试你的建议,比如qt,它仍然可以获取所有内容,选择任何小文件夹,它应该在几秒钟内完成,对吗?不是10-20分钟。我一开始没有回购协议的克隆。嗨,我没有从这个链接复制解决方案。但我确实在完全克隆(获取)回购协议上运行了它。是的,跑步不需要10-20分钟,谢谢!我想避免完整克隆(获取),一定有办法吧?我在问题中发布的命令已经做到了这一点,唯一的问题是它只获得
master
分支,我不知道为什么当我用
git-pull--depth=1 origin-master
替换
git-pull--depth=1 origin 2.7
时,它会提取其他文件夹中的所有文件,当我拉除
master
@Shuman以外的分支时,似乎稀疏签出配置不起作用。请查看您放入
.git/info/sparse checkout
中的内容。它以
cpython/tree/2.7/…
开头,这意味着您已经从分支
2.7
中提取了。现在将其与
--depth=1
相结合。它应该会起作用。
git read-tree -mu HEAD
# 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
git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "finisht/*" >> .git/info/sparse-checkout
git branch -b <your branch>
git pull --depth=1 origin <your branch>