一鱼多吃,git 多仓库提交和 git proxy 实战手册,关键时刻能续命

git 想必大家都不陌生,但下面 2 种场景您有没有遇到过呢?

1,有一个项目,但是想推到 2 个仓库(例如一个 gitlab,一个 github)

2,网络被禁了,但是有一个中间机器是 ok 的,想做个跳板跳出去

如果刚好你也有上述需求或者想储备一下该技能,请往下继续看,建议收藏备用。

git 多仓库提交

多仓库提交常见的有 3 种方式,我一般使用后 2 种(第 2 种)

1,使用 git remote add 命令

2,使用 git remote set-url 命令

3,修改配置文件

remote add 命令

使用 git remote add 命令可以添加多个远程仓库关联,特点是可以同时从多个远程仓库去 pull 和 push,缺点也很明显,就是针对每个远程仓库都需要独立的 pull 和 push,pull 和 push 的时候需要指定名称。

这种方式,我感觉还不如弄多个独立的文件夹分别配置独立远程仓库来得方便,更何况大部分情况我们是期待维护一次 push 到多个仓库,保持多个仓库的一致即可,所以此方式仅做了解吧,反正我不用。

下面的操作会顺便补充一点 remote 相关的操作,可以扩充一下技能,比如 rm,rename 之类的。

以下操作以:https://github.com/bytesops/images 为例进行。

# 首先克隆项目到本地
$ git clone git@github.com:bytesops/images.git
$ cd images

# 查看当前项目远程仓库详情
$ git remote -v
origin  git@github.com:bytesops/images.git (fetch)
origin  git@github.com:bytesops/images.git (push)

# 使用如下命令再添加一个远程仓库
$ git remote add gitlab git@gitlab.com:bytesops/images.git

# 再次查看
$ git remote -v
origin  git@github.com:bytesops/images.git (fetch)
origin  git@github.com:bytesops/images.git (push)
gitlab  git@gitlab.com:bytesops/images.git (fetch)
gitlab  git@gitlab.com:bytesops/images.git (push)

# 想把 origin 更改为 github 方便识别
# 演示一下改名
$ git remote rename origin github

# 再次执行
$ git remote -v
github  git@github.com:bytesops/images.git (fetch)
github  git@github.com:bytesops/images.git (push)
gitlab  git@gitlab.com:bytesops/images.git (fetch)
gitlab  git@gitlab.com:bytesops/images.git (push)

# 查看 .git/config 文件可以看到
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[branch "main"]
        remote = github
        merge = refs/heads/main
[remote "gitlab"]
        url = git@gitlab.com:bytesops/images.git
        fetch = +refs/heads/*:refs/remotes/gitlab/*
[remote "github"]
        url = git@github.com:bytesops/images.git
        fetch = +refs/heads/*:refs/remotes/github/*

注意 config 文件中 有 2 哥 remote 块,即对应我们的 2 个远程仓库,而本地 branch 只有一个 main, 并且当前 main 分支默认指向的是 github 远程分支,所以你当前执行 pull 和 push 都是针对的 github.

上面也说了,这种方式如果想同时同步多个分支,需要主动切换

# 切换至 gitlab 远程仓库 对应 main 分支
$ git push --set-upstream gitlab main

# 查看 config
$ cat .git/config 
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[branch "main"]
        remote = gitlab
        merge = refs/heads/main
[remote "gitlab"]
        url = git@gitlab.com:bytesops/images.git
        fetch = +refs/heads/*:refs/remotes/gitlab/*
[remote "github"]
        url = git@github.com:bytesops/images.git
        fetch = +refs/heads/*:refs/remotes/github/*

如果再需要切回来,还需要执行

$ git push --set-upstream github main

你学废了吗?

好了,我们先删掉 gitlab 远程分支,我所需要的是我只需要维护 github 分支,gitlab 分支只接受提交即可

# 删掉 gitlab 远程仓库
$ git remote rm gitlab

# 查看
$ git remote -v
github  git@github.com:bytesops/images.git (fetch)
github  git@github.com:bytesops/images.git (push)

remote set-url 命令

使用 set-url --add 方式可以添加多个 push 目标地,符合我们的需求。

$ git remote set-url --add github git@gitlab.com:bytesops/images.git

$ git remote -v
github  git@github.com:bytesops/images.git (fetch)
github  git@github.com:bytesops/images.git (push)
github  git@gitlab.com:bytesops/images.git (push)

$ cat .git/config 
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "github"]
        url = git@github.com:bytesops/images.git
        fetch = +refs/heads/*:refs/remotes/github/*
        url = git@gitlab.com:bytesops/images.git
[branch "main"]
        remote = github
        merge = refs/heads/main

大家可以看到 remote 中有 2 个 push ,pull(fetch) 只会从第一个配置 url 里拉取。

看起来 github 里有个 gitlab 有点怪怪的,还是 rename 一下吧,这才是最终想要的样子。

$ git remote rename github origin

$ git remote -v
origin  git@github.com:bytesops/images.git (fetch)
origin  git@github.com:bytesops/images.git (push)
origin  git@gitlab.com:bytesops/images.git (push)

修改配置文件

这个感觉都不用讲了,上面步骤中都带大家看了 .git/config 文件了,如果不想使用命令操作,直接改配置文件一样的效果,因为最终命令行也是生效到配置文件,你可以体验一步到位的感觉。

$ cat .git/config 

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git@github.com:bytesops/images.git
        url = git@gitlab.com:bytesops/images.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main

git 代理配置

如题,工作学习中还会遇到网络不好或者被限制的场景,例如公司限制访问 github ,这时我们毫不迟疑的想到是不是可以使用代哩解决呢?当然可以,一起来体验一下吧。

git clone 有两种形式

1,一个是走 http(s)

2,另一种是 ssh

两种方式的代理设置不一样,两种方式示例如下:

# 使用 ssh 方式(Clone with SSH)
$ git clone git@github.com:bytesops/images.git
# 使用 https 方式(Clone with HTTPS)
$ git clone https://github.com/bytesops/images.git

http 方式

http 代哩设置比较简单:

下面用到的代哩是上一篇文章中配置 docker 拉取镜像介绍过的,传送门:终极解决 docker 拉取镜像失败的问题,其他类似场景也适用文章浏览阅读173次,点赞4次,收藏3次。在安装完 docker 之后,发现 镜像拉取不下来,经过多番踩坑尝试,终于破解了镜像拉取失败问题,希望能够分享给大家,让大家愉快的 codinghttps://blog.csdn.net/radapp/article/details/141648287?spm=1001.2014.3001.5502

# git 设置 https 代哩(上一篇文章中搭建的 polipo 袋里)
$ git config --global https.proxy http://127.0.0.1:8123
# 或者走socks-5 代哩(上一篇文章搭建的 小飞机 客户端端口 1080)
$ git config --global https.proxy socks5://127.0.0.1:1080
# 设置了 https 代哩后即可通过 https 方式 clone 代码
$ git clone https://github.com/bytesops/images.git
# 另附:取消 https 代哩命令,如下
$ git config --global --unset https.proxy

ssh 方式

修改 ~/.ssh/config 文件(不存在则新建)

# 修改~/.ssh/config文件
$ vim ~/.ssh/config

填入以下下配置,注意 方式1(socat)、方式2(nc) 开启一个即可

如当前开启方式2,走 socks5 代理(如 Shadowsocks)

# 必须是 github.com
Host github.com
   HostName github.com
   User git
   # 方式1:走 HTTP 代理
   # ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=8123
   # 方式2:走 socks5 代理(如 Shadowsocks,1080)
   ProxyCommand nc -v -x 127.0.0.1:1080 %h %p

总结

好了,今天主要分享了 git 多仓库提交和 git proxy 相关的知识,内容不多,但是非常实用,应对一些非常规的需求还是很解渴的。

欢迎大家 关注,转发,收藏,互动,有任何问题或需求,可以观注:新质程序猿,找到我,有问必答,一起成长。

  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值