Git使用[原意要新建分支提交但是误提交到原来分支恢复办法]
原意要新建分支提交但是误提交到原来分支恢复办法随着项目越来越复杂,分支越来越多,很多时候是在原来的项目分支简单修改好代码后推送到新的项目分支提交,但是这时候经常会犯错把应该推到新项目分支的代码又推到原来的项目分支了,这里介绍怎么恢复还原!这里想要把GM001还原回2026-05-28这次提交,然后再把2026-07-08这次提交推送到GM02分支,涉及危险操作,雷达不动先复制一份文件夹虽然没有作用但是可以防止后悔一个上午:接下来使用git reset commit指令恢复把代码恢复到上次提交:AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitlog-5commit ab77da246830bf3d85809a92cb40cbe74293eacc(HEAD -GM001, origin/GM001)Author: myfffffffffffff16c.comDate: Wed Jul810:57:1620260800 完善基本功能 commit 6e5ff3cb3967092913a5ddbc221a38edffcc4bd1 Author: myfffffffffffff16c.comDate: Thu May2810:30:3120260800 修改了USB相关的操作可以实现使用OTA升级 AdministratorHCAIA-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 AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstatus On branch GM001 Your branch is behindorigin/GM001by1commit, and can be fast-forwarded.(usegit pullto update yourlocalbranch)Changes not stagedforcommit:(usegit add file...to update what will be committed)(usegit 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指令把最后修改的代码推到堆栈中:AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstash list AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstash warning:inthe working copy ofaaaa/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升级 AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstash list stash{0}: WIP on GM001: 6e5ff3cb 修改了USB相关的操作可以实现使用OTA升级接下来把上一次的commit强制推送到远端让GM001的代码恢复正常:AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstatus On branch GM001 Your branch is behindorigin/GM001by1commit, and can be fast-forwarded.(usegit pullto update yourlocalbranch)nothing to commit, working tree clean AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitlog-2commit 6e5ff3cb3967092913a5ddbc221a38edffcc4bd1(HEAD -GM001)Author: myfffffffffffff16c.comDate: Thu May2810:30:3120260800 修改了USB相关的操作可以实现使用OTA升级 commit 114f8983d4479684c17bdd330969fd6796f763a5 Author: myfffffffffffff16c.comDate: Thu May2810:02:0520260800 更新了版本号 AdministratorHCAIA-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)AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitlog-2commit 6e5ff3cb3967092913a5ddbc221a38edffcc4bd1(HEAD -GM001, origin/GM001)Author: myfffffffffffff16c.comDate: Thu May2810:30:3120260800 修改了USB相关的操作可以实现使用OTA升级 commit 114f8983d4479684c17bdd330969fd6796f763a5 Author: myfffffffffffff16c.comDate: Thu May2810:02:0520260800 更新了版本号这样GM001分支的操作就完成了,接下来先把堆栈中的数据恢复出来:AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstash list stash{0}: WIP on GM001: 6e5ff3cb 修改了USB相关的操作可以实现使用OTA升级 AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstash pop On branch GM001 Your branch is up todatewithorigin/GM001.Changes not stagedforcommit:(usegit add file...to update what will be committed)(usegit 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(usegit addand/orgit commit -a)Dropped refs/stash{0}(8e64c48929356d6cc762e1677325c04a08ab53fc)AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc(GM001)$gitstatus On branch GM001 Your branch is up todatewithorigin/GM001.Changes not stagedforcommit:(usegit add file...to update what will be committed)(usegit 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(usegit addand/orgit commit -a)接下来就是常规的动作,把最后修改的代码切换到新的分支并且提交:AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)$ git branchGM001AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM001)$ git checkout -b GM02Switched to a new branch ‘GM02’AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)$ git statusOn branch GM02Changes 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.hmodified: aaaa/component/xc6xx_drivers/Drivers/USB/usb_driver/xc_hal_usb.cmodified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_adc.cmodified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_fmc_spi.cmodified: aaaa/component/xc6xx_drivers/Drivers/xc_driver/xc_drv_uart.cno changes added to commit (use “git add” and/or “git commit -a”)AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)$ git add .AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)$ git commit -m “完善基本功能”[GM02 a1af6585] 完善基本功能33 files changed, 514 insertions(), 340 deletions(-)AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)$ git pushfatal: The current branch GM02 has no upstream branch.To push the current branch and set the remote as upstream, usegit push --set-upstream origin GM02To have this happen automatically for branches without a trackingupstream, see ‘push.autoSetupRemote’ in ‘git help config’.AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)$ git push --set-upstream origin GM02Enumerating objects: 130, done.Counting objects: 100% (130/130), done.Delta compression using up to 20 threadsCompressing 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 624274b3remote: Create a pull request for ‘GM02’ on Gitee by visiting:remote: https://gitee.com/ceeeeee/ccccccccccc/pull/new/ceeeeee:GM02…ceeeeee:masterTo https://gitee.com/ceeeeee/ccccccccccc.git[new branch] GM02 - GM02branch ‘GM02’ set up to track ‘origin/GM02’.AdministratorHCAIA-WAQHGBEKP MINGW64 /d/xxxx/nnn/mmm/GM02/ccccccccccc (GM02)$ git stash list到这里整个工作就做完了:  