远程仓库更新导致push代码失败


参考自:

  1. git push错误failed to push some refs to的解决方法
  2. 使用git stash命令保存和恢复进度

2023年10月26日


push 出错

可恶,之前在 GitHub 上在线修改了 notes 仓库里的文件,而我在本地又修改了许多代码,导致我把本地代码 push 到 GitHub 上时出错了

错误信息:

To https://github.com/Ratherthan17/my-website.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/Ratherthan17/my-website.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决办法:

1. 先将工作区中未完成的内容添加到暂存区

1先add

2. 查看本地库与哪些远程库有联系

2输入git-remote--v

3. 保存未完成的工作进度

3输入git-stash-save

3输入git-stash-save-效果

4. 把远程库中的更新合并到本地库中

4输入git-pull-rebase-origin-branch

5. 恢复之前保存的工作进度

5输入git-stash-pop

6. 解决冲突

6解决冲突-1

6解决冲突-2-点击标红的地方

6解决冲突-3-选择要保留的内容2

保留远程

保留本地

本地和远程都保留

使用VSCode保留远程

使用VSCode保留本地

使用VSCode保留远程和本地

7. 取消暂存

6取消add

1先add

6修改少了