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

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

接下来使用"git reset commit"指令恢复把代码恢复到上次提交:
bash
Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git log -5
commit ab77da246830bf3d85809a92cb40cbe74293eacc (HEAD -> GM001, origin/GM001)
Author: my <fffffffffffff@16c.com>
Date: Wed Jul 8 10:57:16 2026 +0800
完善基本功能
commit 6e5ff3cb3967092913a5ddbc221a38edffcc4bd1
Author: my <fffffffffffff@16c.com>
Date: Thu May 28 10:30:31 2026 +0800
修改了USB相关的操作,可以实现使用OTA升级
Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git reset 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)
$ git status
On branch GM001
Your branch is behind 'origin/GM001' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." 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
这时候git将恢复到上一次提交并且代码把最新提交的代码恢复到未提交的状态,接下来使用"git stash"指令把最后修改的代码推到堆栈中:
bash
Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git stash list
Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git stash
warning: in the 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 next time Git 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)
$ git stash list
stash@{0}: WIP on GM001: 6e5ff3cb 修改了USB相关的操作,可以实现使用OTA升级
接下来把上一次的commit强制推送到远端让GM001的代码恢复正常:
bash
Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git status
On branch GM001
Your branch is behind 'origin/GM001' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git log -2
commit 6e5ff3cb3967092913a5ddbc221a38edffcc4bd1 (HEAD -> GM001)
Author: my <fffffffffffff@16c.com>
Date: Thu May 28 10:30:31 2026 +0800
修改了USB相关的操作,可以实现使用OTA升级
commit 114f8983d4479684c17bdd330969fd6796f763a5
Author: my <fffffffffffff@16c.com>
Date: Thu May 28 10:02:05 2026 +0800
更新了版本号
Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git push -f
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
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)
$ git log -2
commit 6e5ff3cb3967092913a5ddbc221a38edffcc4bd1 (HEAD -> GM001, origin/GM001)
Author: my <fffffffffffff@16c.com>
Date: Thu May 28 10:30:31 2026 +0800
修改了USB相关的操作,可以实现使用OTA升级
commit 114f8983d4479684c17bdd330969fd6796f763a5
Author: my <fffffffffffff@16c.com>
Date: Thu May 28 10:02:05 2026 +0800
更新了版本号
这样GM001分支的操作就完成了,接下来先把堆栈中的数据恢复出来:
bash
Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git stash list
stash@{0}: WIP on GM001: 6e5ff3cb 修改了USB相关的操作,可以实现使用OTA升级
Administrator@HCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)
$ git stash pop
On branch GM001
Your branch is up to date with 'origin/GM001'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." 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
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)
$ git status
On branch GM001
Your branch is up to date with 'origin/GM001'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." 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
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
到这里整个工作就做完了:

