推送错了仓库的解决办法

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

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

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

相关推荐
一灯架构3 小时前
90%的人答错!一文带你彻底搞懂ArrayList
java·后端
踏着七彩祥云的小丑3 小时前
pytest——Mark标记
开发语言·python·pytest
Dream of maid3 小时前
Python12(网络编程)
开发语言·网络·php
W23035765733 小时前
经典算法:最长上升子序列(LIS)深度解析 C++ 实现
开发语言·c++·算法
Y4090013 小时前
【多线程】线程安全(1)
java·开发语言·jvm
不爱吃炸鸡柳4 小时前
Python入门第一课:零基础认识Python + 环境搭建 + 基础语法精讲
开发语言·python
布局呆星4 小时前
SpringBoot 基础入门
java·spring boot·spring
minji...4 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
Dxy12393102164 小时前
Python基于BERT的上下文纠错详解
开发语言·python·bert
风吹迎面入袖凉4 小时前
【Redis】Redisson的可重入锁原理
java·redis