[英]How do I checkout a sub directory in a huge git repo with specified branch and with sparse checkout?
例如,我想獲取此文件夾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
,但這有點違背了稀疏結賬的目的,我的 inte.net 很慢,回購協議很大。 我怎樣才能得到分支 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
不遵守稀疏結帳配置?
您的檢出失敗是因為提取(並因此獲取)顯式ref 僅提取該ref,因此在您初次提取后,您的repo僅具有refs/heads/master
和refs/remotes/origin/master
,都指向同一提交。 結帳2.7無效,因為您的存儲庫中沒有該名稱的任何內容。
Pull進行合並,並且放置在工作樹中的額外內容git pull origin 2.7
可以解決沖突,合並無法確定正確的結果,因此您必須這樣做。 您會看到不是簽出Tools目錄之外的所有內容,而是簽出了沖突的文件。 我不確定與淺層提取和稀疏檢出合並應該如何整體表現,但是在這里唯一需要做的就是解決沖突。
進行一次淺引用提取就像git一樣輕巧,如果一次性使用帶寬確實如此,那么您可以克隆到ec2實例並標記特定的樹。
嘗試這個
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“另一個/子/樹” >> .git / info / sparse-checkout
然后
git pull origin master
首先設置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
您必須創建一個本地分支以供參考。 更新的步驟應為:
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>
我想從一個分支下載特定文件夾而不下載主存儲庫的整個歷史記錄,因為該回購有大量的歷史記錄和分支數量
mkdir testFolder
cd testFolder
git init
git remote add origin <URL>
以下 git 命令將獲取以 f_2 開頭的分支,例如:f_23、f_24 等。
git config remote.origin.fetch +refs/heads/f_2*:refs/remotes/origin/f_2*
git fetch --depth=1
設置要檢出的文件夾名稱。
git sparse-checkout set <folderName>
下面的命令將從 f_23 分支下載該特定文件夾
git checkout f_23
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.