当我们使用 git clone 尝试从远程克隆仓库的时候,会将远程仓库的全部内容克隆下来,但是有时我们只想获取最新的仓库内容,而并不需要获取其他分支和历史信息,此时我们可以执行选项 git clone --depth 1来只克隆最近一次提交的仓库。

git clone --depth 1 git@github.com/git/git

通过添加选项 --depth 1可以在克隆的时候只克隆最新的记录而不克隆git仓库中的历史记录,从而减少克隆大小,大大提高克隆速度。
但如果我们想要重新克隆完整仓库时,除了去除--depth 1选项外。
我们可以修改配置:

PS D:\Desktop\code\git> git config --local -l
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
remote.origin.url=https://github.com.cnpmjs.org/git/git.git
remote.origin.fetch=+refs/heads/master:refs/remotes/origin/master
branch.master.remote=origin
branch.master.merge=refs/heads/master
PS D:\Desktop\code\git> git config --local remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
PS D:\Desktop\code\git>

通过 git config --local remote.origin.fetch +refs/heads/*:refs/remotes/origin/*可以将原有 fetch 只拉取主分支的代码修改为拉取全部远程分支的代码。
在这里插入图片描述

我们还可以修改git本地仓库中的配置文件
在这里插入图片描述
修改文件中对 fetch 操作的选项,由原有的 +refs/heads/master:refs/remotes/origin/master 修改为 +refs/heads/*:refs/remotes/origin/*,然后回到上一层,重新 git fetch 即可。
在这里插入图片描述
通过这样修改可以使得 git fetch 原本只会 拉取 master 分支,转为拉取远程的全部分支。从而获得完整的远程仓库。

除此之外我们还可以在使用 git fetch --unshallow 的方式拉取远程仓库的全部提交记录,--unshallow 选项与 --depth 选项的作用刚好相反。--depth 会拉取指定的最近的提交记录,而--unsahllow 则会拉取当前分支的全部的提交记录。与上面修改fetch选项区别是,--unshallow选项只会拉取当前分支的提交记录,而不会拉取其他分支。所以如果需要全部其他分支的话还是修改配置比较靠谱。
在这里插入图片描述

其他选项

git fetch -p拉取远程分支前清除本地已经不存在的分支
git fetch -v 显示拉取分支的详细信息

参考资料

在 git clone 时添加了 --depth 后,如何重新拉取所有全部的历史来获取完整的仓库
git clone --depth=1之后,获取全部分支
git clone --depth=1 后获取其他分支

Logo

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

更多推荐