Git-Updates were rejected 解决

Git-Updates were rejected 解决

1. 杂话

 大伙儿应该都用过Git吧,具体是个啥东西我就不说了哈。之前我在用git push的时候遇到了这个报错,我仔细思考了一下,这个问题如果不出意外的话,那么应该就是出现了冲突。那咱们来说说这个问题应该怎么解决。

2. 问题

 这通常发生在git push指令的后面,我举个例子哈。比如说原本你的远程仓库是版本A,比如说你在本地的版本做了修改B,但是同时呢,你又在GitHub的页面上做了修改C。
 那么现在呢,你的本地版本是A+B,远程仓库的版本是A+C。那么这个时候你想把A+B推送过去,你以为是在覆盖A(也就是只需要加上B就行),其实是在覆盖A+C,所以就会有冲突。
 咱们来看一下典型的报错:

error: failed to push some refs to 'https://gitee.com/xxxxxxxx'
To https://gitee.com/xxxxxxxx
!	refs/heads/master:refs/heads/master	[rejected] (fetch first)
hint: Updates were rejected because the remote contains work that you do
Done
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拒绝了你的推送请求。

3. 解决

3.1 拉取远程的最新版本(A+C)

git pull origin <你的分支名,一般是master或者main>

3.2 解决可能的冲突

 如果拉取操作导致了冲突,就需要打开有冲突的文件(也就是B和C更改中都有但是不一样的文件),手动解决冲突并保存文件。Git会在有冲突的文件中标记出冲突的部分:
 选择要保留的更改,然后删除冲突标记(<<<<<<< HEAD、=======、>>>>>>> remote/branch)。完成后保存文件。

3.3 提交

git add .
git commit -m "Update Again"

3.4 再次推送

git push origin <你的分支名,一般是master或者main>
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值