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)

相关推荐
汀、人工智能32 分钟前
报错error: RPC failed,curl 16 Error in the HTTP2 framing layer解决方法
网络·git·网络协议·rpc
Eoneanyna2 小时前
QT设置git仓库
开发语言·git·qt
+码农快讯+2 小时前
Git入门学习(1)
git·学习·elasticsearch
VaporGas6 小时前
idea集成和使用Git指南
java·git·intellij-idea
无名之逆6 小时前
计算机专业的就业方向
java·开发语言·c++·人工智能·git·考研·面试
大广-全栈开发7 小时前
centos 7 安装gitlab
linux·git·centos
zhangphil7 小时前
git删除本地分支报错:error: the branch ‘xxx‘ is not fully merged
git
Tammyhlf8 小时前
新电脑工作流搭建记录-前端篇
git
6230_9 小时前
git使用“保姆级”教程1——简介及配置项设置
前端·git·学习·html·web3·学习方法·改行学it
为祖国添砖爪哇11 小时前
【Git原理与使用】多人协作与开发模型(2)
git