推送错了仓库的解决办法

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

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

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

相关推荐
hoiii1874 小时前
C# 俄罗斯方块游戏
开发语言·游戏·c#
huaqianzkh4 小时前
WinForm + DevExpress 控件的「完整继承关系」
开发语言
二哈喇子!4 小时前
Java Web项目怎么创建 & 没有出现web.xml的解决方法
java·web·web.xml
一起养小猫4 小时前
LeetCode100天Day13-移除元素与多数元素
java·算法·leetcode
a***59264 小时前
C++跨平台开发:挑战与解决方案
开发语言·c++
小北方城市网4 小时前
Spring Security 认证授权实战(JWT 版):从基础配置到权限精细化控制
java·运维·python·微服务·排序算法·数据库架构
青槿吖4 小时前
Java 集合操作:HashSet、LinkedHashSet 和 TreeSet
java·开发语言·jvm
刘联其4 小时前
Prism Region注册父子区域 子区域初始化导航没生效解决
java·开发语言
CoderCodingNo4 小时前
【GESP】C++六级考试大纲知识点梳理, (5) 动态规划与背包问题
开发语言·c++·动态规划
移幻漂流4 小时前
Lua脚本的游戏开发优势与应用开发局限:技术对比与行业实践深度解析
开发语言·junit·lua