git push时出现 error: failed to push some refs ... hint: Updates were rejected because the remote ...

本文记录了在使用Git时遇到的推送失败问题,由于远程仓库已有更新,导致本地与远程版本不匹配。解决方法是先通过`git pull`合并远程更改,再执行`git push`。遵循这一流程,成功地将本地更改推送到远程仓库。
摘要由CSDN通过智能技术生成

1.问题如下:

echigh@huangchenggong:/mnt/e/work/workspace/withdraw_validdata_from_file/withdraw$ git push -u origin main
Username for 'https://github.com': echigh
Password for 'https://echigh@github.com':
To https://github.com/echigh/withdraw_frontbytes_of_file.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/echigh/withdraw_frontbytes_of_file.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.

2.原因是远程仓库有被更改过,和本地任一commit版本不匹配

参考:【狀況題】怎麼有時候推不上去... - 為你自己學 Git | 高見龍

3.解决办法:

1)先git pull合并远程的更改,然后再git push

2)直接用git push -f 强制覆盖远程仓库,我反正是不敢

4.于是,我是这样做的:

echigh@huangchenggong:/mnt/e/work/workspace/withdraw_validdata_from_file/withdraw$ git pull
remote: Enumerating objects: 22, done.
remote: Counting objects: 100% (22/22), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 21 (delta 13), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (21/21), 5.03 KiB | 66.00 KiB/s, done.
From https://github.com/echigh/withdraw_frontbytes_of_file
   c6d3d5e..e50b1bc  main       -> origin/main
Merge made by the 'recursive' strategy.
 README.md | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 README.md

然后再push:

echigh@huangchenggong:/mnt/e/work/workspace/withdraw_validdata_from_file/withdraw$ git push
Username for 'https://github.com': echigh
Password for 'https://echigh@github.com':
Enumerating objects: 44, done.
Counting objects: 100% (42/42), done.
Delta compression using up to 8 threads
Compressing objects: 100% (38/38), done.
Writing objects: 100% (38/38), 3.16 MiB | 994.00 KiB/s, done.
Total 38 (delta 19), reused 0 (delta 0)
remote: Resolving deltas: 100% (19/19), completed with 2 local objects.
To https://github.com/echigh/withdraw_frontbytes_of_file.git
   e50b1bc..b7684a5  main -> main

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值