背景:
有些新手在提交本地代码到github或者国内的代码仓库码云的时候通常会报以下错误
$ git push -u origin master
To gitee.com:zrxisme/testLog.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@gitee.com:zrxisme/testLog.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.
错误原因:
原因是因为,远程仓库里的文件,在本地没找到,也就是说远程仓库里的内容比本地仓库还要新,这是由于多人合作开发,其他小伙伴写完内容后提交到远程仓库,你没有把最新版本pull下来,或者你现在使用版本是老版本
解决办法:
在每次提交前先使用git pull 命令把远程仓库里最新的内容拿下来,然后再执行push操作,git pull相当于把最新代码拿下来并于现有代码进行合并
其他错误:
$ git pull origin masterFrom gitee.com:zrxisme/testLog
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
原因:
使用pull操作的时候出现以上错误,原因很简单,你提交的内容和远程仓库里的内容不同的,有些小伙伴很奇怪,我明明从远程仓库clone下来的,为什么说是两个不同的内容呢?其实这是新手经常犯的一个错误,大多数情况都是由于在提交的时候重复使用了git init操作,这导致重新生成了一个新的git文件夹,之前的git文件夹被覆盖,所有之前提交的历史被清空,所以当我们clone项目的时候,重新编写提交切记不要使用git init操作
解决办法:
执行以下操作git pull origin master --allow-unrelated-histories,执行后悔打开一个vi文本编辑器输入:wq!退出即可,
然后重新提交,你会发现成功了
后序:
如果还有遇到其他问题欢迎留言,如果我可以解决的,一定会给你一定帮助,希望能帮助到您!