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)

相关推荐
Bigger11 小时前
为什么你的 Git 提交需要签名?—— Git Commit Signing 完全指南
git·开源·github
DianSan_ERP1 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
红豆子不相思1 天前
Tomcat 环境搭建与集群实战
服务器·git·tomcat
杰哥技术分享1 天前
Git 仓库迁移技术文档:从 CODING.net 迁移至腾讯云 CNB
git
梅孔立1 天前
Ansible 100 台服务器一键管控实战 进阶版
服务器·git·ansible
qq_426003962 天前
git切换当前分支到远程分支
git
ON10N2 天前
100% 纯 Vibe Coding,我是怎么用 AI 撸出一个 VS Code 插件的
git·ai编程·visual studio code
Lunar*2 天前
告别臃肿!使用 git-filter-repo 优雅清理 Git 历史记录
git
tq10862 天前
agent 记忆 = markdown + json + git
人工智能·git
何以不说话3 天前
DevOps、Git 和 GitLab
git·gitlab·devops