git-clone的single-branch操作回退

(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)

最近使用git越来越多,一些git的功能使用也更熟悉了一些。

之前使用了single-branch下载分支,后来想取消掉,但怎么做呢,查了一些资料之后,了解到了怎么做,特记录下来。

背景

所在的项目中,一个库的分支非常多,有50上以上的分支,clone工程时,也耗时比较长。下载的时候,考虑着只用一个分支,就指定了分支clone的工程。

操作语句形如:

$ git clone -b mybranch --single-branch --depth 1 https://user@192.168.0.101:8080/scm/git/demoproject local-folder-name

过了一段时间后,发现单用这一个分支不够用了,需要下载一个新的分支,但通过

git branch -a 查看是,会发现本地和远端都只有这一个分支存在。

形如:

$ git branch

*mybranch

remotes/origin/mybranch

如何去除single-branch影响呢?怎么能看到远端的所有分支呢?也只有看到了远端的哪些其余的分支,才能够去把远端的其他分支下载到本地来,才能checkout来switch到另一个分支上去。

去除single-branch影响

如何去除single-branch影响?我们先来看一看single-branch会对配置产生哪些影响呢?

观察发现,在.git/config有体现它的影响。

下面观察一个带single-branch 和 一个不带single-branch是,这个配置文件的变化。

观察配置

不使用single-branch时的,.git/config缺省配置:

复制代码
[remote "origin"]
        url = http://username@192.168.0.101:8080/scm/git/demoproject
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "mybranch"]
        remote = origin
        merge = refs/heads/mybranch

使用single-branch时的,.git/config配置

复制代码
[remote "origin"]
        url = http://username@192.168.0.1014:8080/scm/git/demoproject
        fetch = +refs/heads/mybranch:refs/remotes/origin/mybranch
[branch "mybranch"]
        remote = origin
        merge = refs/heads/mybranch

可以观察到差异体现在remote "origin"里面的fetch配置项上。

回退配置

对于回退时,查阅资料发现,也确实是来修改这个配置项的,下面来看修改的方式。

  1. 设定配置[remote "origin"].fetch

$ git config remote.origin.fetch "+refs/heads/:refs/remotes/origin/"

  1. 重新获取origin信息,通过fetch获取到所有的分支信息树结构
    注意:这一步如果库比较大,分支比较多,可能耗时会比较长

$ git fetch origin

  1. 观察远端分支,会看到增加了很多

$ git branch -a

*mybranch

remotes/origin/mybranch

remotes/origin/master

...

去除single-branch后

经过上一步,回退了single-branch。

此时这个本地库就和直接clone的一样了,可以切换到拥有的所有分支了。

当然随之而来,也会看到本地库所占的大小增加了很多。

linux下可以使用 du -sh 来查看目录所占的大小,如果观察的话,会发现增长了很多。

因为去除single-branch的话,我们就和远程库保持一致了,自然大小也会比较大了。

有利有弊,去除single-branch后,大小增加了,但同时我们恢复了切换到所有其它分支的自由。

切换到其他分支方法:

此时如果想要checkout到一个新的分支上,就可以执行下面操作来做了:

$ git checkout master

分支 master 设置为跟踪来自 origin 的远程分支 master

切换到一个新分支 'master'

切换后,就可以查看本地分支,可以看到本地多了这个分支,也切换到了这个分支

$ git branch

  • master
    mybranch

(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)

相关推荐
Vermouth_001 天前
git clone的时候提示access denied
git
qq_437657271 天前
清楚本地的git并重新登录
git
jiang_changsheng1 天前
工作流agent汇总分析 2
java·人工智能·git·python·机器学习·github·语音识别
顶点多余1 天前
版本控制器-git
linux·git
夔曦1 天前
Git工程日常下拉/上传完整流程(自用)
git
岱宗夫up1 天前
GitHub Desktop如何设置中文?这不是个简单问题
git·github
岱宗夫up1 天前
.env 文件是干啥的?为什么不能提交到 Git?
大数据·git·elasticsearch·搜索引擎·gitee·github·gitcode
家里有只小肥猫2 天前
git回退某条/多条提交记录
git
何中应2 天前
IDEA 中让 Git 忽略 .idea 目录
java·git·intellij-idea
小柯博客2 天前
从零开始打造 OpenSTLinux 6.6 Yocto 系统 - STM32MP2(基于STM32CubeMX)(六)
c语言·git·stm32·单片机·嵌入式硬件·开源·yocto