【问题标题】:How do I checkout a sub directory in a huge git repo with specified branch and with sparse checkout?如何签出具有特定分支和稀疏签出的巨大 git 存储库中的子目录?
【发布时间】:2016-03-05 22:16:34
【问题描述】:

比如我想获取这个文件夹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,它只会在 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 不尊重稀疏结帐配置?

【问题讨论】:

    标签: git bash branch


    【解决方案1】:

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

    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>
    

    【讨论】:

      【解决方案2】:

      您的结帐失败,因为拉取(并因此获取)显式引用 获取该引用,因此在您初始拉取后,您的仓库只有 refs/heads/masterrefs/remotes/origin/master,两者都指向同一个犯罪。 2.7 的结帐不起作用,因为您的存储库没有该名称的任何内容。

      Pull 进行合并,工作树中的额外内容git pull origin 2.7 用于解决冲突,合并无法确定正确的结果,因此您必须这样做。您会看到,并非“工具”目录之外的所有内容都被检出,只有冲突的文件被检出。我不确定与浅提取和稀疏结帐的合并应该如何整体表现,但要求解决冲突肯定是这里唯一要做的事情。

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

      【讨论】:

        【解决方案3】:

        试试这个

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

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

        git config core.sparseCheckout true
        

        现在定义您想要的文件夹。这是完成添加它。 git/info/sparse-checkout,

        echo "some/dir/" >> .git/info/sparse-checkout echo "another/sub/tree" >> .git/info/sparse-checkout

        然后

        git pull origin master
        

        【讨论】:

        • 谢谢,但整个想法是不要使用 -f "fetch"?因为在我的低速互联网上获取速度非常慢。它仍然可以获得我不需要的所有数据吗?
        【解决方案4】:

        首先设置config参数:

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

        在 .git/info/sparse-checkout 中配置 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
        

        【讨论】:

        • 你是说我应该运行这个吗?刚试过不工作。你能给出完整的命令吗? pastebin.com/W4yiJhwe 我明白你的意思,你一定是从这里复制的:blogs.atlassian.com/2014/05/handle-big-repositories-git 但整个想法是一开始不要克隆,我已经提到我想避免克隆或获取整个 repo。您可以在 qt 等大型 repo 上尝试您的建议,它仍然可以获取所有内容,选择任何小文件夹,它应该在几秒钟内完成,对吧?不是 10-20 分钟。
        • 我一开始没有回购的克隆。
        • 嗨,我没有从这个链接复制解决方案。但我确实在一个完整的克隆(获取)存储库上运行了它。是的,运行它不应该需要 10-20 分钟,
        • 谢谢!我想避免完整的 clone(fetch) ,一定有办法吗?我在我的问题中发布的命令已经这样做了,唯一的问题是它只获得master 分支,我不知道为什么当我用git pull --depth=1 origin 2.7 替换git pull --depth=1 origin master 时,它会拉取其他文件夹中的每个文件,当我拉除master 以外的分支时,似乎稀疏结帐配置不起作用
        • @Shuman 看看你在.git/info/sparse-checkout 中的内容。它以cpython/tree/2.7/...... 开头,这意味着您已经从分支2.7 拉出。现在将其与--depth=1 结合起来。它应该可以工作。
        猜你喜欢
        • 2020-02-21
        • 1970-01-01
        • 2018-01-30
        • 2013-02-15
        • 2018-01-23
        • 2016-07-08
        • 1970-01-01
        • 2018-12-19
        • 1970-01-01
        相关资源
        最近更新 更多