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)

相关推荐
前端Hardy34 分钟前
杀疯了!Git 2.54 正式发布,3个封神新特性,效率直接翻倍!
git
Eloudy2 小时前
迁移带有 git lfs 功能的 github 仓库
git·github
xlq223223 小时前
1.git
git
运维全栈笔记5 小时前
零基础掌握Jenkins CI/CD:Java项目自动构建与部署全流程指南
git·servlet·ci/cd·gitee·自动化·jenkins·devops
菜萝卜子6 小时前
【Git】GitLab 18.9 全局服务器钩子(Server Hooks)官方规范与落地实践
服务器·git·gitlab
用户11481867894847 小时前
Git Stash 丢失后的完整找回指南
前端·git
Ting.~7 小时前
GIT详解
java·笔记·git
克拉拉KLARA7 小时前
vscode禁用在git提交中插入ai coauthor copilot
git·vscode·copilot
水无痕simon8 小时前
05 Git 基础 – 查看提交历史
git
谢斯9 小时前
【GIT】子模块初始化与排错记录
git