簡體   English   中英

如何淺克隆深度為 1 的特定提交?

[英]How to shallow clone a specific commit with depth 1?

是否可以淺克隆存儲庫中的特定提交,即深度為 1? 就像是

git clone http://myrepo.git 728a4d --depth 1

獲取存儲庫狀態,因為它在提交時使用 SHA 728a4d...

動機是避免克隆整個存儲庫,然后檢查該特定提交,當我們只對特定提交的存儲庫狀態感興趣時。

從 Git 2.5.0(需要在客戶端和服務器端可用)開始,您可以在服務器端設置uploadpack.allowReachableSHA1InWant=true以啟用特定 SHA1 的獲取:

git init
git remote add origin <url>
git fetch --depth 1 origin <sha1>
git checkout FETCH_HEAD

請注意,我沒有找到直接使用git clone執行此操作的語法。

注意:我的示例無助於通過提交哈希克隆到,但它有助於克隆標簽並擁有輕量級存儲庫。

如果您的“克隆”中只有一個提交,並且您打算使用提交哈希,那么簡短的回答是NO

我使用這個命令構造(在v2.13.2.windows.1測試)作為標簽:

git clone --depth 1 git@github.com:VENDOR/REPO.git --branch 1.23.0 --single-branch

完整示例:

$ git clone --depth 1 git@github.com:Seldaek/monolog.git --branch 1.23.0 --single-branch
Cloning into 'monolog'...
remote: Counting objects: 201, done.
remote: Compressing objects: 100% (188/188), done.
remote: Total 201 (delta 42), reused 32 (delta 5), pack-reused 0
Receiving objects: 100% (201/201), 190.30 KiB | 0 bytes/s, done.
Resolving deltas: 100% (42/42), done.
Note: checking out 'fd8c787753b3a2ad11bc60c063cff1358a32a3b4'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

$ cd monolog

.git目錄大小(使用完整clone 267K2.6M ):

$ du -h --max-depth=0 .git
267K    .git

我想表示,-- --branch可以帶標簽/分支。

https://git-scm.com/docs/git-clone#git-clone---branchltnamegt

--branch還可以獲取標簽並在結果存儲庫中的該提交處分離 HEAD。

UPD

簡而言之,它可以接受“refs”。 您可以在此處閱讀更多信息:git 錯誤消息“服務器不允許請求未通告的對象”是什么意思?

此外,沒有像這樣的技巧:

git fetch --depth 1 origin <COMMIT_HASH>

感謝@BenjiWiebe 指出我的錯誤。

嘗試在 bash 中使用while

git clone --depth=1 $url
i=1; while ! git show $sha1; do git fetch --depth=$((i+=1)); done

這很慢,因為它單獨獲取每個提交; 您可以增加增量(以批量獲取提交並提高網絡性能),但這仍然是一種蠻力方法。

直接的答案是:你不能直接使用 git clone 來做到這一點。
為什么? 可以在此處找到詳細說明: 為什么沒有 Git 克隆特定提交選項?

你還能做什么?

如何將存儲庫克隆到特定的提交? (完整克隆)

# Create empty repository to store your content
git clone <url>
git reset <sha-1> --hard

更多信息:

如何克隆單個分支?

git clone <url> --branch <branch_name> --single-branch <folder_name>

如何僅從給定分支克隆最新提交?

git clone <url> --depth=1 --branch <branch_name> --single-branch <folder_name>

如何淺克隆深度為 1 的特定提交?

正如@sschuberth 評論的那樣: --depth意味着--single-branch

使用 fetch 命令代替克隆:

# fetch a commit (or branch or tag) of interest
# In this case you will have the full history of this commit
git fetch origin <sha1>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM