Git推送Github仓库失败:提示分支已更新但未同步如何解决?
Git推送至Github失败的问题解决
问题背景
已配置两个Git远程仓库:
droplet:部署用的bare仓库,通过Git hooks自动部署到/var/www/myappgithub:Github中心仓库
执行git push droplet可成功推送,但git push github时提示"没有可提交内容,分支已更新",但Github仓库最后一次提交是4个月前,本地提交未同步到Github。
关键信息
Git Remote配置
$ git remote -v droplet admin@droplet-myapp:/home/admin/repo/MyApp.git (fetch) droplet admin@droplet-myapp:/home/admin/repo/MyApp.git (push) github git@github.com-MyApp:MyApp/MyApp.git (fetch) github git@github.com-MyApp:MyApp/MyApp.git (push)
Git Config配置
$ more config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "github"] url = git@github.com-MyApp:MyApp/MyApp.git fetch = +refs/heads/*:refs/remotes/github/* [user] name = MyApp email = florent@myapp.com [branch "master"] [remote "droplet"] url = admin@droplet-myapp:/home/admin/repo/MyApp.git fetch = +refs/heads/*:refs/remotes/droplet/* [branch "master"] remote = droplet merge = refs/heads/master
失败的推送操作
$ git add . && git commit -m 'fix' && git push github On branch master Your branch is up-to-date with 'droplet/master'. nothing to commit, working tree clean
问题原因
- 本地
master分支的跟踪目标是droplet/master(从Git Config的最后一个[branch "master"]配置可看出),Git判断分支是否"已更新"仅与droplet/master对比,而非github/master。 - 本地提交已推送到
droplet,但未同步到github,导致Github仓库内容落后于本地。
解决方案
方法1:直接指定推送分支(快速解决)
执行以下命令,强制将本地master分支推送到Github的master分支:
git push github master:master
方法2:修复分支跟踪配置(长期优化)
步骤1:合并重复的分支配置
编辑.git/config文件,将两个重复的[branch "master"]节点合并,可按需设置默认推送行为:
[branch "master"] remote = droplet merge = refs/heads/master # 可选:设置默认推送的远程仓库为github pushremote = github
步骤2:设置多远程同步推送(可选)
如果希望每次推送到droplet时自动同步到Github,可给droplet远程添加额外推送地址:
git remote set-url --add droplet git@github.com-MyApp:MyApp/MyApp.git
此后执行git push droplet时,代码会同时推送到droplet和github两个仓库。
方法3:确认提交差异(排查用)
先查看本地master比Github的master多了哪些提交:
git log --oneline github/master..master
若输出有提交记录,说明本地确实存在未推送到Github的内容,再执行方法1的推送命令即可。
内容的提问来源于stack exchange,提问作者Florent
相关产品推荐
相关产品推荐

