介绍git clone --depth=1的用法

情况一:git clone

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

 

使用git可视化命令git log --graph --oneline --all,查看仓库的所有历史提交记录(我这里用alias取了别名glog)



说明:一般仓库文件不大时,我们都可以用这个方法git clone仓库,但问题是有时候,在仓库历史的某次commit时,有人不小心提交了1G的文件,虽然后面的commit中他把这个文件删除了,但是在.git文件夹中仍然存储着这个文件,所以如果我们克隆仓库这个仓库,会把所有的历史协作记录都clone下来,这样整个文件会非常大,其实对于我们直接使用仓库,而不是参与仓库工作的人来说,只要把最近的一次commit给clone下来就好了。这就好比一个产品有很多个版本,我们只要clone最近的一个版本来使用就行了。实现这个功能就需要用到git clone --depth=1命令


情况二:git clone --depth=1

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

 

最近一次commit

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

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

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

 

 

enligh分支的最近一次commit

总结:

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

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

enligh分支的最近一次commit

上面提到的 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

 


Reference

喜欢这篇文章?欢迎打赏~~

 

posted @ 2020-11-19 20:25  苍青浪  阅读(6992)  评论(0编辑  收藏  举报