git本地仓库是可以与多个远程仓库关联的,如果想知道怎么配置,请参考Git如何使用多个托管平台管理代码

git remote关联了多个远程仓库时,总会遇到一些问题。今天就遇到了两个远程仓库不一致导致无法push的情况。

远程仓库间出现差异

大概情况是这样的,我是一个本地仓库关联了githubgitee两个远程仓库。

git remote add all git@github.com:cumt-robin/BlogFrontEnd.git
git remote set-url --add all git@gitee.com:tusi/BlogFrontEnd.git

由于不小心在远程仓库gitee上手动修改了README.md文件,导致两个远程仓库出现了差异。所以当我在本地完成了一部分功能,准备提交到远程仓库时,出现了报错。

$ git push all --all
Everything up-to-date
To gitee.com:tusi/BlogFrontEnd.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@gitee.com:tusi/BlogFrontEnd.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方案

由于是gitee的仓库多修改了一点东西,因此在本地再加一个remote,单独关联gitee

$ git remote add gitee git@gitee.com:tusi/BlogFrontEnd.git

gitee的代码拉到本地master

$ git pull gitee master
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (1/1), done.
From gitee.com:tusi/BlogFrontEnd
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> gitee/master
Already up to date!
Merge made by the 'recursive' strategy.

再将本地master推送到远程all

$ git push all --all
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 6 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 499 bytes | 499.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:cumt-robin/BlogFrontEnd.git
   1557ece..8391333  master -> master
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 6 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 917 bytes | 917.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: Powered By Gitee.com
To gitee.com:tusi/BlogFrontEnd.git
   8912ff5..8391333  master -> master

问题得以解决!


首发连接


扫一扫下方小程序码或搜索Tusi博客,即刻阅读最新文章!

Tusi博客


程序员白彬
669 声望19 粉丝

全网同名,前端原创分享,常年坚持记录和分享,阅读量超百万。