今天git push origin master 报错:
$ git push origin master
To github.com:xiaoyangLee/LearnJava.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:xiaoyangLee/LearnJava.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.
原因是远程仓库中的文件和我们本地的仓库有差异,例如你的远程仓库有个文件Readme.md,但是本地仓库却没有,就可能会出现这种情况。
之所以出现这种情况,原因多种多样,例如你直接使用了github上传文件,或者像我一样,这次使用了另外一个系统推送了文件,导致了此问题的出现。解决办法显然有,把本地仓库删了再git clone一个,但是这种方法显然不可取….
解决办法:使用git pull 合并分支
git pull --rebase origin master
git pull命令用于从另一个存储库或本地分支获取并集成(整合),取回远程主机某个分支的更新,再与本地的指定分支合并。