git上传文件到GitHub报错: Updates were rejected because the tip of your current branch is behind的解决方法
步骤如下:
如果在第一步中创建时已经初始化过项目,则这时会提醒 error: failed to push some refs to ‘https://github.com/yourname/test.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文件了,所以需要先pull下来。命令如下: $ git pull origin master
这时又会报错:fatal: refusing to merge unrelated histories
说这两个库有不相干的历史记录而无法合并,这时我们可以加上一个参数 --allow-unrelated-histories 即可成功pull: $ git pull origin master --allow-unrelated-histories
注意:
但是这时会可能会提示必须输入提交的信息,默认会打开vim编辑器,先按 i 切换到插入模式,写完后 Esc→:→wq 即可保存退出编辑器。如果不进入vim编辑器,则会自动生成一个合并代码的commit。然后再使用前面的命令push将本地提交推送到远程仓库。
后面如果本地还有commit,就可以直接用 git push origin master 推送。