前言:

本文以GitHub仓库 https://github.com/labuladong/fucking-algorithm 为例,对比介绍git clone 和 git clone --depth=1的用法。

一. git clone

git clone https://github.com/labuladong/fucking-algorithm

当项目过大时,git clone时会出现error: RPC failed; HTTP 504 curl 22 The requested URL returned error: 504 Gateway Time-out的问题,如下图
在这里插入图片描述

原因分析:

如果我们执行git clone 这个仓库时,会把所有的历史协作记录都clone下来,这样整个文件会非常大,其实对于我们直接使用仓库,而不是参与仓库工作的人来说,只要把最近的一次commit给clone下来就好了。这就好比一个产品有很多个版本,我们只要clone最近的一个版本来使用就行了 。实现这个功能就需要用到git clone --depth=1命令

二. git clone --depth=1

git clone https://github.com/labuladong/fucking-algorithm.git --depth=1

我们只克隆下包含最近一次commit的一个分支,这样这个项目文件就不会很大

而如果我们想只克隆某个指定分支的最近一次commit,可以使用下面命令:

git clone  https://github.com/labuladong/fucking-algo --depth=1  --branch=merge_branch

三. 总结:

  • 用 git clone --depth=1 的好处是限制 clone 的深度,不会下载 Git 协作的历史记录,这样可以大大加快克隆的速度
  • depth用于指定克隆深度,为1即表示只克隆最近一次commit
  • 适合用 git clone --depth=1 的场景:你只是想clone最新版本来使用或学习,而不是参与整个项目的开发工作

四. git clone --depth=1后拉取其他分支的方法

上面提到的 git clone --depth=1 操作只会clone一个分支english,如果我们想把其他远程分支(如master)也克隆到本地,我们需要用下面的命令:

$ git remote set-branches origin 'remote_branch_name'
$ git fetch --depth 1 origin remote_branch_name
$ git checkout remote_branch_name
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐