推送错了仓库的解决办法

今天在本地调好代码之后,准备把代码提交到仓库中,在命令行出现了以下报错:

bash 复制代码
To github.com:iamxurulin/static-resource.git
! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:iamxurulin/static-resource.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.

我开始以为是GitHub(远程仓库)上有一些本地仓库里没有的更新,所以执行了强制推送的指令:

bash 复制代码
git push -f origin main

执行完操作后,我就跑去源代码仓库查看,发现没有提交记录。

后面跑去static-resource仓库一看才发现,刚才把代码全部提交到这个仓库了,并且git 的推送机制把本地历史记录强制同步到了远程,把这个仓库原有的commit记录全部覆盖了,想通过SHA哈希值恢复到以前的版本也恢复不了。

怎么办呢?

GitHub 个人动态在执行强制推送时,会记录这一动作,但是我打开 GitHub 个人主页在页面中间的 Contribution activity(贡献动态)中也没有找到最近几小时的commit记录。

别急,Git 是有"后悔药"的。

既然有两个仓库,本地电脑上是不是应该有两个文件夹?

我进入到存放 static-resource 的文件夹,右键 -> Git Bash Here ,输入 git log -1

这时,屏幕上显示了 commit xxxxxxxx

接下来,我在这个文件夹打开终端,输入:

bash 复制代码
git push -f https://github.com/username/static-resource.git xxxxxxxx:main

之后,打开static-resource仓库,发现已经恢复了。

所以,后续推送的时候记得执行:

bash 复制代码
git remote -v

确认一下要推送到的远程仓库。

相关推荐
西红柿炒番茄312 分钟前
【Python】一个自动切换壁纸的python程序
开发语言·python
ShiJiuD6668889995 分钟前
JSP Cookie和Session
java·开发语言
geNE GENT10 分钟前
Spring Boot 实战篇(四):实现用户登录与注册功能
java·spring boot·后端
FQNmxDG4S9 小时前
Java多线程编程:Thread与Runnable的并发控制
java·开发语言
前端老石人9 小时前
HTML 字符引用完全指南
开发语言·前端·html
matlab_xiaowang9 小时前
Redux 入门:JavaScript 可预测状态管理库
开发语言·javascript·其他·ecmascript
虹科网络安全9 小时前
艾体宝干货|数据复制详解:类型、原理与适用场景
java·开发语言·数据库
axng pmje10 小时前
Java语法进阶
java·开发语言·jvm
rKWP8gKv710 小时前
Java微服务性能监控:Prometheus与Grafana集成方案
java·微服务·prometheus
老前端的功夫10 小时前
【Java从入门到入土】28:Stream API:告别for循环的新时代
java·开发语言·python