背景
本地git库,要push到gitlab上,执行完如下命令后报错:
git push origin master
报错信息为:
![rejected] master -> master(non-fast-forward)
error:failed to push some refs to XXX
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,但本地仓库中并没有该文件。
解决方案
添加忽略不相干的历史选项,将远程仓库中的文件重新拉取到本地,这样本地文件会出现README.md:
git pull origin master --allow-unrelated-histories
之后再正常推送:
git push origin master
总结
- 远程仓库在上传的时候,添加了README.md文件,而本地没有该文件,导致本地仓库和远程仓库存在不同的commit,导致无法提交。
- 先创建远程仓库,然后clone到本地,再进行开发
- 远程仓库不添加README.md,如果有需要,本地新建后push到远程仓库即可