bash 如何在不可靠的连接上克隆大型 Git 存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9268378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I clone a large Git repository on an unreliable connection?
提问by khunshan
I want to clone LibreOffice. From the official website, this is what's written:
我想克隆 LibreOffice。官网上是这样写的:
All our source code is hosted in git:
Clone:
$ git clone git://anongit.freedesktop.org/libreoffice/core# (browse)Clone (http):
$ git clone http://anongit.freedesktop.org/git/libreoffice/core.git# slowerTarballs:
http://download.documentfoundation.org/libreoffice/src/please find the latest versions (usually near the bottom)
我们所有的源代码都托管在 git 中:
克隆:
$ git clone git://anongit.freedesktop.org/libreoffice/core#(浏览)克隆 (http):
$ git clone http://anongit.freedesktop.org/git/libreoffice/core.git# 更慢压缩包:
http://download.documentfoundation.org/libreoffice/src/请找到最新版本(通常在底部附近)
now, when I write this command in git bash to clone, it starts fetching. But the repository is so big that after hours I lose connectivity for a few seconds, it rolls back the download, and I get nothing.
现在,当我在 git bash 中编写此命令进行克隆时,它开始获取。但是存储库太大了,几个小时后我失去连接几秒钟,它回滚下载,我什么也没得到。
Is there any way I can download the repository smoothly even if interruptions occur?
即使发生中断,有什么方法可以顺利下载存储库?
P.S. I am a new user of Git and I use a 1 MB DSL internet connection. The repository must be over 1 GB.
PS 我是 Git 的新用户,我使用 1 MB DSL 互联网连接。存储库必须超过 1 GB。
采纳答案by Sylvain Defresne
The repository is accessible via the httpprotocol (aka dumb protocol) here: http://anongit.freedesktop.org/git/libreoffice/core.git.
该存储库可通过http此处的协议(又名哑协议)访问:http: //anongit.freedesktop.org/git/libreoffice/core.git。
You can download everything here with wgetor another download manager, and you'll have a clone of the repository. After that, you rename the directory from core.gitto .git, and use the following command to tell git about the remote url:
您可以使用wget或其他下载管理器在此处下载所有内容,并且您将拥有存储库的克隆。之后,您将目录从core.gitto重命名.git,并使用以下命令告诉 git 远程 url:
$ git remote add remote http://anongit.freedesktop.org/git/libreoffice/core.git
$ git reset --hard HEAD
回答by Paul Nikonowicz
do 'git clone --depth 100' It should grab the last 100 commits
do 'git clone --depth 100' 它应该抓取最后 100 次提交
回答by Rich
You can do the following:
您可以执行以下操作:
git clone --depth 1 [email protected]:User/Project.git .
git fetch --unshallow
The first clonewill still be atomic, so if your connection is not reliable enough to fetch the current HEAD then you will have trouble.
第一个clone仍然是原子的,所以如果你的连接不够可靠,无法获取当前的 HEAD,那么你就会遇到麻烦。
The subsequent fetchshould be incremental and retryable if the connection drops half-way though.
fetch如果连接中途断开,后续应该是增量和可重试的。
回答by Andriy Makukha
The best method that I know of is to combine shallow clone(--depth 1) feature with sparse checkout, that is checking out only the subfolders or files that you need. (Shallow cloning also implies --single-branch, which is also useful.) See udondan's answerfor an example.
我所知道的最好的方法是将浅克隆( --depth 1) 功能与稀疏检出结合起来,即仅检出您需要的子文件夹或文件。(浅克隆也意味着--single-branch,这也很有用。)请参阅udondan 的答案以获取示例。
Additionally, I use a bash loop to keep retrying until finished successfully. Like this:
此外,我使用 bash 循环不断重试,直到成功完成。像这样:
#!/bin/bash
git init <repo_dir>
cd <repo_dir>
git remote add origin <repo_url>
# Optional step: sparse checkout
git config core.sparsecheckout true # <-- enable sparse checkout
echo "subdirectory/*" >> .git/info/sparse-checkout # <-- specify files you need
# Keep pulling until successful
until $( git pull --depth=1 origin master ); do # <-- shallow clone
echo "Pulling git repository failed; retrying..."
done
In this way I can eventually pull large repos even with slow VPN in China…
通过这种方式,即使在 CN 使用速度较慢的 VPN,我最终也可以拉出大型存储库……
Importantly, by pulling this way you will still be able to push.
重要的是,通过这种方式拉动,您仍然可以推动。
回答by j4v4m4n
I used a my web hosting server with shell access to clone it first and then used rsync to copy it locally. rsync would copy only remaining files when resumed.
我使用了我的具有外壳访问权限的网络托管服务器来首先克隆它,然后使用 rsync 将其复制到本地。rsync 在恢复时只会复制剩余的文件。
回答by Swapnil Naukudkar
Increase buffer size so that git can utilize your bandwidth properly. Use following commands.
增加缓冲区大小,以便 git 可以正确利用您的带宽。使用以下命令。
git config --global core.compression 0
git config --global http.postBuffer 1048576000
git config --global http.maxRequestBuffer 100M
git clone <repo url>
Wait till clone get complete.
等到克隆完成。

