我们在使用Git分布式管理工具的时候,经常会涉及到如何将本地最新代码切换到之前版本历史里指定版本(这里可能是代码出现bug,需求变更,版本分支,产品线分支等..),这是我在实际开发中遇到过的问题,所以在此记录一下整个操作的过程,方便下次能直接使用,而不是去百度里翻来翻去,还有就是百度到的东西不一定对,一定要结合自己的问题进行取舍,不然后悔的总是自己,记得有一次我直接把整个本地版本库都弄乱了,害我又重新写了一个代码,闲话不说了,直接上操作步骤:
1. 先重新clone远程最新代码到另一个文件目录下(避免文件项目名冲突)。
93287@DESKTOP-0413I27 MINGW64 /d/workspaceMaster-DongNaoVip2018-04-09
$ git clone https://gitee.com/qrmc/his_pay.git
Cloning into 'his_pay'...
remote: Counting objects: 4154, done.
remote: Compressing objects: 100% (3254/3254), done.
remote: Total 4154 (delta 2066), reused 150 (delta 28)
Receiving objects: 100% (4154/4154), 4.12 MiB | 1.12 MiB/s, done.
Resolving deltas: 100% (2066/2066), done.
2. 将新项目当前版本回退到历史版本节点上(这里可以根据自己提交代码时的描述进行选择回退,所以在开发中尽量把提交代码时的描述写清楚,方便以后版本的回退),这里回退的版本编号:e29440fbf364ca29656e42769d342c34b279bbc9,在回退时只需要填写前6位(e29440)就能回退版本。
93287@DESKTOP-0413I27 MINGW64 /d/workspaceMaster-DongNaoVip2018-04-09/his_pay (master)
$ git checkout e29440
Note: checking out 'e29440'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b
HEAD is now at e29440f... 分界线=======分布式事务处理
3. 创建一个新的分支(e29440)来保留切换后的版本分支。
93287@DESKTOP-0413I27 MINGW64 /d/workspaceMaster-DongNaoVip2018-04-09/his_pay ((e29440f...))
$ git checkout -b e29440
Switched to a new branch 'e29440'
4. 查看本地分支情况,当前是:e29440。
93287@DESKTOP-0413I27 MINGW64 /d/workspaceMaster-DongNaoVip2018-04-09/his_pay (e29440)
$ git branch
* e29440
master
5. 查看远程分支情况,当前是:master。
93287@DESKTOP-0413I27 MINGW64 /d/workspaceMaster-DongNaoVip2018-04-09/his_pay (e29440)
$ git branch -r
origin/HEAD -> origin/master
origin/master
6. 在远程仓库创建新分支(分支名:e29440),并将当前本地分支(e29440)提交到远程分支上,这个时候就能在远程仓库看到分支和分支对应的项目了。
93287@DESKTOP-0413I27 MINGW64 /d/workspaceMaster-DongNaoVip2018-04-09/his_pay (e29440)
$ git push origin e29440
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by Gitee.com
To https://gitee.com/qrmc/his_pay.git
* [new branch] e29440 -> e29440
7. 远程仓库有多个版本,如何下载指定版本到本地,-b表示要从分支下载,slave就是具体的某个分支的名称 。 clone时创建新的分支(slave)替代默认Origin HEAD(master) 。
93287@DESKTOP-0413I27 MINGW64 /d/workspaceMaster-DongNaoVip2018-04-09/ym_his_pay
$ git clone -b slave https://gitee.com/qrmc/his_pay.git
Cloning into 'his_pay'...
remote: Counting objects: 4226, done.
remote: Compressing objects: 100% (3311/3311), done.
remote: Total 4226 (delta 2091), reused 149 (delta 28)
Receiving objects: 100% (4226/4226), 4.13 MiB | 22.00 KiB/s, done.
Resolving deltas: 100% (2091/2091), done.
Checking connectivity... done.
93287@DESKTOP-0413I27 MINGW64 /d/workspaceMaster-DongNaoVip2018-04-09/ym_his_pay/his_pay (slave)
$ git branch
* slave
8. 修改当前分支名用 git branch -m 新分支名,也可以通过 git branch -m 原分支名 新分支名。
93287@DESKTOP-0413I27 MINGW64 /d/workspaceMaster-DongNaoVip2018-04-09/ym_his_pay/his_pay (slave)
$ git branch -m ymSlave
93287@DESKTOP-0413I27 MINGW64 /d/workspaceMaster-DongNaoVip2018-04-09/ym_his_pay/his_pay (ymSlave)
$ git branch
* ymSlave