Git中一些报错的解决
本文发布于362天前,最后更新于362天前,其中的信息可能有所发展或是发生改变。如有疑问请联系邮箱:admin@yemengstar.com。

参考文章:

error: failed to push some refs to ‘xxxx

场景

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.

原来使用的提交命令是 git push -u origin 分支名,不妨尝试一下使用覆盖提交的方式 git push -f origin 分支名,其中“-f”是覆盖提交的参数。

如果需要,也可以先拉取代码,再推送代码:

先:git pull

后:git push

以上使用了强制push的方式提交代码,这样会带来版本覆盖的问题。比如:

fatal: refusing to merge unrelated histories

出错原因是两个仓库不同而导致的,需要在后面加上--allow-unrelated-histories进行允许合并,即可解决问题

完整代码如下:

#如果之前有初始化 init 需要删除
 
rm -rf .git
 
#初始化本地仓库(不要总是初始化)
 
git init
 
#连接远程git仓库
 
git remote add origin 仓库地址(注意是带有.git结尾的地址)
 
#创建并切到分支
git checkout -b 分支名
 
#添加本地需要提交的代码(.表示所有)
git add .
 
#提交代码并添加说明
git commit -m "说明内容"
 
#上传代码代码到分支(首次要先用git pull下拉代码)
git push origin 分支名

后记

当然,如果你想完全避免这个问题,新建一个仓库再上传就好啦

本文为夜梦星尘原创文章。
文章作者:夜梦星尘
文章链接:Git中一些报错的解决
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自夜梦星尘
支持作者:夜梦星尘的爱发电
上一篇
下一篇