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)

相关推荐
Rsun0455119 小时前
Git相关面试题
git
rogerogers20 小时前
在 VS Code Remote SSH 中完美配置 GPG 自动签名 (macOS 到 Ubuntu 24.04)
git·visual studio code
qiuyuyiyang21 小时前
Nginx 反向代理之upstream模块以及完整配置反向代理示例
git·nginx·github
勇闯逆流河1 天前
【Linux】Linux基础开发工具(git、dbg)
linux·运维·服务器·开发语言·c++·git
Wpa.wk1 天前
IDE中配置Git常见操作
ide·经验分享·git·测试工具
NaCl鱼呜啦啦2 天前
Git 仓库损坏修复指南:从诊断到恢复
git
huazi992 天前
AI编程(一):Trae+Git 应用开发
git·ai编程·trae
badhope2 天前
GitHub热门AI技能Top20实战指南
前端·javascript·人工智能·git·python·github·电脑
m0_635647482 天前
git管理github上的repository(三)
git·github
圥忈&&丅佽&&扗虖2 天前
ubuntu 安装git
linux·git·ubuntu