这几天项目中遇到一个git场景:
项目仓库保存在以下三个地方:
- GitHub
- GitLab
- Coding
由于项目是我在开发维护,所以这三个仓库地址一直都是在同一个远程仓库origin下,三处代码时刻保持一致。
>>> git remote -v
origin https://github.com/myname/project.git (fetch)
origin https://github.com/myname/project.git (push)
origin http://10.8.15.49:9091/myname/project.git (push)
origin https://e.coding.net/myname/01/project.git (push)
但最近项目代码需要进行一处修改,需要同事来做,同事不会使用Git,所以就直接让他在Coding上进行修改了。但如何把他做出的修改更新到GitHub和GitLab上,使三处代码继续保持一致呢?
首先,添加新的远程仓库
>>> git remote add coding https://e.coding.net/myname/01/project.git
>>> git remote -v
coding https://e.coding.net/myname/01/project.git (fetch)
coding https://e.coding.net/myname/01/project.git (push)
origin https://github.com/myname/project.git (fetch)
origin https://github.com/myname/project.git (push)
origin http://10.8.15.49:9091/myname/project.git (push)
origin https://e.coding.net/myname/01/project.git (push)
>>> cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/myname/project.git
fetch = +refs/heads/*:refs/remotes/origin/*
url = http://10.8.15.49:9091/myname/project.git
url = https://e.coding.net/myname/01/project.git
[branch "main"]
remote = origin
merge = refs/heads/main
[branch "1.4"]
remote = origin
merge = refs/heads/1.4
[remote "coding"]
url = https://e.coding.net/myname/01/project.git
fetch = +refs/heads/*:refs/remotes/coding/*
可以看到已经多出coding远程仓库了
然后,使用fetch更新一下
>>> git fetch coding
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 9 (delta 7), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), 810 bytes | 67.00 KiB/s, done.
From https://e.coding.net/myname/01/project
* [new branch] 1.4 -> coding/1.4
* [new branch] main -> coding/main
然后将代码合并,上传
>>> git checkout 1.4
>>> git merge coding/1.4
>>> git push origin 1.4
大功告成!!!
之前一直都是手动修改.git/config文件来添加删除远程仓库地址,但最近才知道git提供有专门的命令
>>> git remote set-url origin --add https://e.coding.net/myname/01/project.git
效果就是会在.git/config中的origin远程仓库下添加一个url
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/MacwinWin/wxapp_api.git
fetch = +refs/heads/*:refs/remotes/origin/*
url = http://10.8.15.49:9091/gaoxiang/wxapp_api.git
url = https://e.coding.net/xlxdsj/01/wxapp_api.git
[branch "main"]
remote = origin
merge = refs/heads/main
[branch "1.4"]
remote = origin
merge = refs/heads/1.4
参考:
https://*.com/a/12795747/7151777
https://www.jianshu.com/p/4cd46619b3a5
https://*.com/a/16608774/7151777