解决error: failed to push some refs to ‘https://github.com...‘问题

在尝试push代码到远程仓库时遇到错误,提示本地有未提交的更改。使用gitstash保存未提交更改,然后执行gitpull合并远程更新。之后,使用gitstashpop恢复本地更改并进行提交,成功推送代码。
摘要由CSDN通过智能技术生成

问题描述

本地修改代码后正准备push到远程仓库,但是遇到了如下问题:

error: failed to push some refs to 'https://github.com...'
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 pull获得

ou@ous-MacBook-Pro demo % git pull
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

这是因为在执行 git pull 命令时,Git 会自动尝试合并远程仓库的更改和本地仓库的更改,如果本地存在未提交的更改,可能会导致代码冲突或其他问题。输入git pull --rebase origin master无法解决问题

ou@ous-MacBook-Pro demo % git pull --rebase origin master
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

解决方案

我选择先保存未提交的更改,使用 git stash 命令将这些更改保存到一个栈中,以便稍后重新应用这些更改。执行以下命令:

# 先保存到一个栈
ou@ous-MacBook-Pro demo % git stash
Saved working directory and index state WIP on main: ...

# 合并远程仓库的更改和本地仓库的更改
ou@ous-MacBook-Pro demo % git pull

# 将更改恢复回来
ou@ous-MacBook-Pro demo % git stash pop

# 提交本地仓库更改
ou@ous-MacBook-Pro demo % git push

至此,问题就解决了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值