git 版本回滚
回滚到之前的版本
首先,要回滚到之前提交的版本,必须要知道都提交了那些,可以使用git log
查看已经提交的版本信息。
$ git log
commit 6e4898ea6d9b5151c8e0be41cbeca0263983ea9c (HEAD -> master)
Author: hc1609 <hc_1609@163.com>
Date: Sat Dec 15 15:45:42 2018 +0800
use <if>,<where>,<set>,<choose>,<trim> and <foreach> optimize
select,update and insert
commit 842f74c037dac2acd6703431885afc8377511fe3
Author: hc1609 <hc_1609@163.com>
Date: Thu Dec 13 20:40:56 2018 +0800
Exercise of the second chapter
commit d5cc0b921c27f4d0f340fbaf53a62e5b9c26f59f
Author: hc1609 <hc_1609@163.com>
Date: Wed Dec 12 00:22:54 2018 +0800
practice on the machine of the chapter II
commit 0f1c3ca20cc5be5d4f807e9cb254d09cfd56615a
Author: hc1609 <hc_1609@163.com>
Date: Sun Dec 9 11:25:10 2018 +0800
:
其中commit后面的一长串字符串是每次提交后自动为本次提交的生成的id,如6e4898ea6d9b5151c8e0be41cbeca0263983ea9c
,
(HEAD -> master)
表示的是当前HEAD是哪个提交。
git log - -pretty=oneline
只查看commit的id和注释。
$ git log --pretty=oneline
6e4898ea6d9b5151c8e0be41cbeca0263983ea9c (HEAD -> master) use <if>,<where>,<set>,<choose>,<trim> and <foreach> optimize select,update and insert
842f74c037dac2acd6703431885afc8377511fe3 Exercise of the second chapter
d5cc0b921c27f4d0f340fbaf53a62e5b9c26f59f practice on the machine of the chapter II
0f1c3ca20cc5be5d4f807e9cb254d09cfd56615a the first version,only use mybatis and SqlSession
ad2cf82f4e735c94504fd207a80ceadf46a22819 Initial commit
接下来使用git reset --hard id/HEAD^
进行版本的回滚:
git reset --hard HEAD^
--hard
选项表示彻底将工作区、暂存区和版本库记录恢复到指定的版本库,HEAD^
表示回滚到上一个版本,HEAD^^
表示回滚到上上一个版本,以此类推,如果要回滚到上100个版本,可以使用HEAD~100
。
强制推送到远程分支
在本地回滚后,远程上面依然是之前的版本,用git push -f origin master
将本地库强制推送到远程,这时远程库也已经回滚到之前的提交了。
回到之后的版本
如果在进行版本回退之后,回到以前提交的版本,此时git中还保存着该版本后的版本,版本号通过git reflog
获取到回退之前的版本号,使用git reset --hard 版本号/head^
回到后面的版本。
git reflog
可以查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)