为什么网站显示在建设中,高密做网站的价位,中国菲律宾高铁,企业的网站建设公司Git 是我们开发过程中经常使用到的版本管理工具,在平常情况下我们从远程克隆的时候会将整个库克隆下来#xff0c;这会包括整个版本库的历史提交记录和远程库里的所有分支。但在一些情况下#xff0c;比如我们并不需要查看历史提交记录而只是希望能够获取到最新的代码#x…Git 是我们开发过程中经常使用到的版本管理工具,在平常情况下我们从远程克隆的时候会将整个库克隆下来这会包括整个版本库的历史提交记录和远程库里的所有分支。但在一些情况下比如我们并不需要查看历史提交记录而只是希望能够获取到最新的代码或者我们只希望克隆某个指定分支时而不是克隆全部的远程分支此时我们就可以用到一些选项来减少我们的仓库的体积从而提高生产效率。
通过-b branch_name 选项Git 克隆指定分支
当我们没有通过 -b 选项指定分支时Git 会默认会在本地创建一个 master/main分支并关联到远程的主干分支上但如果我们希望创建的本地分支关联的不是主干分支时我们就可以通过-b 选项来指定我们追踪的远程分支名称如
# 指定克隆远程分支 /develop/branch_1## 标题git clone -b /develop/branch_1 gitwww.gitee.com/ghimi/hello.git-b选项搭配 --single-branch 选项只克隆指定分支
通过这种方式克隆远程仓库除了指定的远程分支不是主干分支以外还是会将远程的所有分支都拉取下来并不能够起到减小克隆仓库体积的功能。
# 指定克隆远程分支 /develop/branch_1git clone -b /develop/branch_1 gitwww.gitee.com/ghimi/hello.gitcd hello
# 进入仓库通过 git branch -r 查看可以看到还是拉取到了全部远程分支git branch -r
origin/20200113-function-concurrency-test
origin/20210112_8457548_basic2
origin/HEAD - origin/master
origin/acl_retrofit
origin/add_api_for_get_organizations如果想要只克隆指定分支还需要搭配 --single-branch 选项这样我们就只会拉取到我们指定的分支而不会拉取到其他远程分支了
# 指定克隆远程分支 /develop/branch_1git clone -b /develop/branch_1 --single-branch gitwww.gitee.com/ghimi/hellocd hello
# 进入仓库通过 git branch -r 当前就只剩下一个分支了git branch -r
/origin/develop/branch_1通过 --depth depth 选项指定历史记录的深度
在一些情况下导致仓库体积过大的原因并不是分支太多而是单个分支下的提交记录过多我们在一些情况下只想查看分支的最新提交的代码此时我们可以--depth 1 实现浅克隆此时我们拉取的代码就是最新一次提交后的代码快照。
# 指定克隆远程分支 /develop/branch_1git clone -b /develop/branch_1 --depth 1 gitwww.gitee.com/ghimi/hellocd hello
# 进入仓库通过 git branch -r 当前只有一个分支了git branch -r
/origin/develop/branch_1
# 通过 git log 查看历史提交记录发现只剩下最后一次的提交记录而无法看到历史的提交记录git log
commit 75bdec69e39ea85fcdc750ef289ece7e76d2b79c (grafted, HEAD - develop/branch_1, origin/develop/branch_1)
Author: ghimi
Date: Fri Mar 3 15:01:36 2023 0800
Merge commit fc3fe2a0002fc394696ec131797038707f5f4864 into /develop/branch_2在指定了 --depth 选项后就无需再指定 --single-branch 选项了。因为浅克隆模式默认隐含了 --single-branch 选项如果想要在 --depth 选项的基础上还拉取其他分支的代码可以通过添加 --no-single-branch 选项此时会拉取到全部远程分支的对应的最近一次的提交记录。
# 指定克隆远程分支 /develop/branch_1git clone -b /develop/branch_1 --depth 1 --no-single-branch gitwww.gitee.com/ghimi/hello.gitcd hello
# 进入仓库通过 git branch -r 可以看到拉取到了全部远程分支git branch -r
origin/develop/branch_1
origin/20200113-function-concurrency-test
origin/20210112_8457548_basic2
origin/HEAD - origin/master
origin/acl_retrofit
origin/add_api_for_get_organizations
# 通过 git log 查看历史提交记录,发现只剩下最后一次的提交记录而无法看到历史的提交记录git log
commit 75bdec69e39ea85fcdc750ef289ece7e76d2b79c (grafted, HEAD - develop/branch_1, origin/develop/branch_1)
Author: ghimi
Date: Fri Mar 3 15:01:36 2023 0800
Merge commit fc3fe2a0002fc394696ec131797038707f5f4864 into /develop/branch_2通过 git fetch --unshallow 恢复全部的历史提交记录
我们在使用 --depth 1 查看到了当前代码后如果想要追溯代码的历史提交记录时可以用 git fetch --unshallow 命令重新拉取指定远程分支的全部历史记录。
# 指定克隆远程分支 /develop/branch_1git clone -b /develop/branch_1 --depth 1 gitwww.gitee.com/ghimi/hellocd hello
# 进入仓库通过 git branch -r 当前只有一个分支了git branch -r
/origin/develop/branch_1
# 通过 git log 查看历史提交记录发现只剩下最后一次的提交记录而无法看到历史的提交记录git log
commit 75bdec69e39ea85fcdc750ef289ece7e76d2b79c (grafted, HEAD - develop/branch_1, origin/develop/branch_1)
Author: ghimi
Date: Fri Mar 3 15:01:36 2023 0800
Merge commit fc3fe2a0002fc394696ec131797038707f5f4864 into /develop/branch_2git fetch --unshallow
remote: Enumerating objects: 79593, done.
remote: Counting objects: 100% (79593/79593), done.
remote: Total 79593 (delta 2162), reused 77796 (delta 2162), pack-reused 0
Receiving objects: 100% (79593/79593), 23.05 MiB | 3.43 MiB/s, done.
Resolving deltas: 100% (2162/2162), done.
From www.gitee.com:ghimi/hello* branch develop/branch_1 - FETCH_HEAD
# 此时通过 git log 命令查看时就会发现能够看到全部的历史提交记录了git log使用 git checkout -t origin/develop/branch_1 在本地创建分支并追踪同名的远程分支
使用 Idea 的同学肯定用过当在 checkout 远程分支后会在本地为远程创建一个同名的分支我们可以在本地分支上完成开发然后推送到相关远程分支上去。其实使用的就是 -t 选项 git checkout -t origin/develop/branch_1
Switched to a new branch develop/branch_1
Branch develop/branch_1 set up to track remote branch develop/branch_1 from origin.参考资料
Git clone 手册