news 2026/7/9 11:13:50

Git使用[原意要新建分支提交但是误提交到原来分支恢复办法]

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Git使用[原意要新建分支提交但是误提交到原来分支恢复办法]

原意要新建分支提交但是误提交到原来分支恢复办法

随着项目越来越复杂,分支越来越多,很多时候是在原来的项目分支简单修改好代码后推送到新的项目分支提交,但是这时候经常会犯错把应该推到新项目分支的代码又推到原来的项目分支了,这里介绍怎么恢复还原!

这里想要把GM001还原回2026-05-28这次提交,然后再把2026-07-08这次提交推送到GM02分支,涉及危险操作,雷达不动先复制一份文件夹虽然没有作用但是可以防止后悔一个上午:

接下来使用"git reset commit"指令恢复把代码恢复到上次提交:

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitlog-5commit ab77da246830bf3d85809a92cb40cbe74293eacc(HEAD ->GM001, origin/GM001)Author: my<fffffffffffff@16c.com>Date: Wed Jul810:57:162026+0800 完善基本功能 commit 6e5ff3cb3967092913a5ddbc221a38edffcc4bd1 Author: my<fffffffffffff@16c.com>Date: Thu May2810:30:312026+0800 修改了USB相关的操作,可以实现使用OTA升级 Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitreset 6e5ff3cb3967092913a5ddbc221a38edffcc4bd1 Unstaged changes after reset: M aaaa/component/xc6xx_drivers/Drivers/CMSIS/Device/xc_m0_register/xc_reg_adc.h M aaaa/component/xc6xx_drivers/Drivers/USB/usb_driver/xc_hal_usb.c M aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_adc.c M aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_fmc_spi.c M aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_uart.c Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstatus On branch GM001 Your branch is behind'origin/GM001'by1commit, and can be fast-forwarded.(use"git pull"to update yourlocalbranch)Changes not stagedforcommit:(use"git add <file>..."to update what will be committed)(use"git restore <file>..."to discard changesinworking directory)modified: aaaa/component/xc6xx_drivers/Drivers/CMSIS/Device/xc_m0_register/xc_reg_adc.h modified: aaaa/component/xc6xx_drivers/Drivers/USB/usb_driver/xc_hal_usb.c modified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_adc.c

这时候git将恢复到上一次提交并且代码把最新提交的代码恢复到未提交的状态,接下来使用"git stash"指令把最后修改的代码推到堆栈中:

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstash list Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstash warning:inthe working copy of'aaaa/project/product/rf24g_only_mouse/project/product/tri_mode_mouse/mdk/tri_mode_mouse.uvprojx', LF will be re placed by CRLF the nexttimeGit touches it Saved working directory and index state WIP on GM001: 6e5ff3cb 修改了USB相关的操作,可以实现使用OTA升级 Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstash list stash@{0}: WIP on GM001: 6e5ff3cb 修改了USB相关的操作,可以实现使用OTA升级

接下来把上一次的commit强制推送到远端让GM001的代码恢复正常:

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstatus On branch GM001 Your branch is behind'origin/GM001'by1commit, and can be fast-forwarded.(use"git pull"to update yourlocalbranch)nothing to commit, working tree clean Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitlog-2commit 6e5ff3cb3967092913a5ddbc221a38edffcc4bd1(HEAD ->GM001)Author: my<fffffffffffff@16c.com>Date: Thu May2810:30:312026+0800 修改了USB相关的操作,可以实现使用OTA升级 commit 114f8983d4479684c17bdd330969fd6796f763a5 Author: my<fffffffffffff@16c.com>Date: Thu May2810:02:052026+0800 更新了版本号 Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitpush-fTotal0(delta0), reused0(delta0), pack-reused0(from0)remote: Powered by GITEE.COM[1.1.23]remote: Set trace flag c44960a7 To https://gitee.com/ceeeeee/ccccccccccc.git + ab77da24...6e5ff3cb GM001 ->GM001(forced update)Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitlog-2commit 6e5ff3cb3967092913a5ddbc221a38edffcc4bd1(HEAD ->GM001, origin/GM001)Author: my<fffffffffffff@16c.com>Date: Thu May2810:30:312026+0800 修改了USB相关的操作,可以实现使用OTA升级 commit 114f8983d4479684c17bdd330969fd6796f763a5 Author: my<fffffffffffff@16c.com>Date: Thu May2810:02:052026+0800 更新了版本号

这样GM001分支的操作就完成了,接下来先把堆栈中的数据恢复出来:

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstash list stash@{0}: WIP on GM001: 6e5ff3cb 修改了USB相关的操作,可以实现使用OTA升级 Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstash pop On branch GM001 Your branch is up todatewith'origin/GM001'.Changes not stagedforcommit:(use"git add <file>..."to update what will be committed)(use"git restore <file>..."to discard changesinworking directory)modified: aaaa/component/xc6xx_drivers/Drivers/CMSIS/Device/xc_m0_register/xc_reg_adc.h modified: aaaa/component/xc6xx_drivers/Drivers/USB/usb_driver/xc_hal_usb.c modified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_adc.c modified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_fmc_spi.c no changes added to commit(use"git add"and/or"git commit -a")Dropped refs/stash@{0}(8e64c48929356d6cc762e1677325c04a08ab53fc)Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstatus On branch GM001 Your branch is up todatewith'origin/GM001'.Changes not stagedforcommit:(use"git add <file>..."to update what will be committed)(use"git restore <file>..."to discard changesinworking directory)modified: aaaa/component/xc6xx_drivers/Drivers/CMSIS/Device/xc_m0_register/xc_reg_adc.h modified: aaaa/component/xc6xx_drivers/Drivers/USB/usb_driver/xc_hal_usb.c modified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_adc.c modified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_fmc_spi.c no changes added to commit(use"git add"and/or"git commit -a")

接下来就是常规的动作,把最后修改的代码切换到新的分支并且提交:

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git branch

  • GM001

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git checkout -b GM02
Switched to a new branch ‘GM02’

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)
$ git status
On branch GM02
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git restore …” to discard changes in working directory)
modified: aaaa/component/xc6xx_drivers/Drivers/CMSIS/Device/xc_m0_register/xc_reg_adc.h
modified: aaaa/component/xc6xx_drivers/Drivers/USB/usb_driver/xc_hal_usb.c
modified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_adc.c
modified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_fmc_spi.c
modified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_uart.c

no changes added to commit (use “git add” and/or “git commit -a”)

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)
$ git add .

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)
$ git commit -m “完善基本功能”
[GM02 a1af6585] 完善基本功能
33 files changed, 514 insertions(+), 340 deletions(-)

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)
$ git push
fatal: The current branch GM02 has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin GM02

To have this happen automatically for branches without a tracking
upstream, see ‘push.autoSetupRemote’ in ‘git help config’.

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)
$ git push --set-upstream origin GM02
Enumerating objects: 130, done.
Counting objects: 100% (130/130), done.
Delta compression using up to 20 threads
Compressing objects: 100% (61/61), done.
Writing objects: 100% (77/77), 84.31 KiB | 6.49 MiB/s, done.
Total 77 (delta 55), reused 12 (delta 10), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag 624274b3
remote: Create a pull request for ‘GM02’ on Gitee by visiting:
remote: https://gitee.com/ceeeeee/ccccccccccc/pull/new/ceeeeee:GM02…ceeeeee:master
To https://gitee.com/ceeeeee/ccccccccccc.git

  • [new branch] GM02 -> GM02
    branch ‘GM02’ set up to track ‘origin/GM02’.

Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)
$ git stash list

到这里整个工作就做完了: ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/d1d11d4a49dc4ae492f1d740d2cc7db5.png) ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/149f3a711273453fa1631f711b0de023.png)
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/9 11:11:55

RT-Thread 启动流程详解:从复位入口到线程调度

RT-Thread 启动流程详解&#xff1a;从复位入口到线程调度 做 RT-Thread 移植或排查启动异常时&#xff0c;最容易卡住的地方往往不是某一行代码&#xff0c;而是“不知道系统现在启动到了哪一步”。本文按实际调试视角&#xff0c;把 RT-Thread 从芯片复位到进入线程调度的主线…

作者头像 李华
网站建设 2026/7/9 11:11:41

秒杀场景缓存设计首选:阿里云 Tair 高性能内存数据库

单 SKU 120 万 QPS、P99 < 1ms、0 超卖、SLA 99.99%——阿里云 Tair 高性能内存数据库是电商秒杀、抢红包、限时抢购等极致并发场景的缓存设计首选方案&#xff0c;相比自建 Redis 与传统 MySQLRedis 架构在原子扣减、防穿透与削峰能力上全面领先。推荐理由&#xff1a; 单 …

作者头像 李华
网站建设 2026/7/9 11:10:24

大厂疯抢AI人才,产品起薪30K!0经验如何快速入行?

AI浪潮下&#xff0c;各个大厂正加速抢夺AI人才。阿里、腾讯、百度、字节跳动等大厂招聘官网累计招聘5W人&#xff0c;其中AI岗位成为主力军&#xff08;部分公司AI人才占比高达75%&#xff09;&#xff0c;包括AI算法、研发、AI产品经理等。 小林刚完成一段实习&#xff0c;因…

作者头像 李华
网站建设 2026/7/9 11:08:26

长沙回南天窗户结露发霉?选对门窗 + 做好这几点,梅雨季也干爽

一到回南天、梅雨季&#xff0c;长沙很多家庭都头疼&#xff1a;窗户玻璃流水、窗边墙体发霉、轨道里积满水&#xff0c;擦都擦不及&#xff0c;时间长了还容易有霉味。不少人以为是自己家里太潮&#xff0c;其实很大原因是门窗保温密封没做好。普通断桥铝门窗隔热条窄、密封差…

作者头像 李华
网站建设 2026/7/9 11:03:40

若依RuoYi容器化实战:Docker Compose一键部署微服务全指南

1. 项目概述&#xff1a;为什么若依必须容器化&#xff1f;——从“部署焦虑”到“一键交付”的真实转变“告别手动部署&#xff01;”这句标题不是营销话术&#xff0c;而是我踩过至少17次坑、重装过5台服务器、在凌晨三点对着报错日志抓狂后&#xff0c;亲手写下的技术宣言。…

作者头像 李华
网站建设 2026/7/9 11:03:11

2小时,我搭了一套设备效率分析看板,设备停机损失一眼看清

很多工厂都知道&#xff0c;设备效率很重要。但一到真正分析的时候&#xff0c;问题就来了&#xff1a;为什么设备天天在开&#xff0c;产量还是上不去&#xff1f;设备数据不能说没有&#xff0c;甚至很多企业的数据还不少。有设备运行记录&#xff0c;有维修工单&#xff0c;…

作者头像 李华