Git 同时与多个远程库互相同步

情形:有两个git服务器,比如github,gitosc,有个项目同时在两个服务器上,要互相同步

其实命令还是比较简单的,比如一个现有的git项目,在github,gitosc中分别创建好对应的项目。

1:移除现在旧有的远程服务器origin

git remote rm origin

2:关联gitosc远程库

git remote add gitosc https://gitee.com/hongdada/learngit.git
git push -u gitosc master

关联github远程库

 git remote add github https://github.com/hongdada/learngit.git
git push -u github master

3.查看远程库信息

λ git remote -v
github  https://github.com/hongdada/learngit.git (fetch)
github  https://github.com/hongdada/learngit.git (push)
gitosc  https://gitee.com/hongdada/learngit.git (fetch)
gitosc  https://gitee.com/hongdada/learngit.git (push)

这样就ok了,就布置好了,下面就是操作

复制代码
D:\代码\Git\learngit
λ git push
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 223 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
To https://gitee.com/hongdada/learngit.git
   a48d040..875d588  master -> master

D:\代码\Git\learngit
λ git push gitosc master
Everything up-to-date

D:\代码\Git\learngit
λ git push github master
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 223 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/hongdada/learngit.git
   a48d040..875d588  master -> master
复制代码

可以看出我第一次是直接git push,没有指定远程库名称,默认推送到了gitosc中,开始还以为一次性推送到了2个服务器呢,剩下的github需要指定名称推送。

如果一次性推送呢

方法一:

复制代码
D:\代码\Git\learngit
λ git remote  rm github

D:\代码\Git\learngit
λ git remote rm gitosc

D:\代码\Git\learngit
λ git remote add all https://gitee.com/hongdada/learngit.git

D:\代码\Git\learngit
λ git remote set-url --add all https://github.com/hongdada/learngit.git
复制代码

推送:

复制代码
D:\代码\Git\learngit
λ git push all --all
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 269 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://gitee.com/hongdada/learngit.git
   af6a587..48a0880  master -> master
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 269 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/hongdada/learngit.git
   af6a587..48a0880  master -> master
复制代码

看到有2个推送说明

修改前打开项目.git文件夹内的config文件

复制代码
[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[branch "master"]
    remote = gitosc
    merge = refs/heads/master
[branch "dev"]
[remote "github"]
    url = https://github.com/hongdada/learngit.git
    fetch = +refs/heads/*:refs/remotes/github/*
[remote "gitosc"]
    url = https://gitee.com/hongdada/learngit.git
    fetch = +refs/heads/*:refs/remotes/gitosc/*
复制代码

 修改后查看:

复制代码
[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[branch "master"]
[branch "dev"]
[remote "all"]
    url = https://gitee.com/hongdada/learngit.git
    fetch = +refs/heads/*:refs/remotes/all/*
    url = https://github.com/hongdada/learngit.git
复制代码

 方法二:根据上面的配置可以引出第二种一起修改多远程的方式,直接修改配置文件.git/config

删除all

git remote rm all

查看配置文件:

复制代码
[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[branch "master"]
[branch "dev"]
复制代码

修改配置文件为:

复制代码
[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[branch "master"]
[branch "dev"]
[remote "all"]  
    url = https://github.com/hongdada/learngit.git 
    url = https://gitee.com/hongdada/learngit.git  
复制代码

推送信息:

复制代码
D:\代码\Git\learngit
λ git push all --all
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 290 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/hongdada/learngit.git
   48a0880..2dab796  master -> master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 290 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://gitee.com/hongdada/learngit.git
   48a0880..2dab796  master -> master
复制代码

 

http://blog.csdn.net/isea533/article/details/41382699

posted @   hongdada  阅读(13882)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2016-09-22 Spring Boot 3 Hibernate
点击右上角即可分享
微信分享提示