一.reset指令,利用commit-id回退
Git回滚版本一般使用commit-id来做,每次commit都会在本地仓库会根据算法生成唯一的40位的哈希值,commit-id来标记每次的提交记录和版本,就是该版本的SNAPSHOT(快照),拿到任意版本的commit-id , 我们可以根据它做版本的回滚,可以回滚到任意版本的SNAPSHOT。
假设回滚到上一个版本,具体做法如下
1.查看日志 git log -3 查看最近3条的提交记录
Administrator@SKY-20180625ODQ MINGW64 /f/git/test/ (master)
$ git log -3
commit 0ce9992b33656243438f740b2044f2c93044e867 (HEAD -> master, origin/master)
Author: mx <????@xx.com>
Date: Thu Dec 20 15:50:08 2018 +0800
3.txt
commit 8215c7f203024f7fee036f56dcf5b5bd4af0f232
Author: mx <????@xx.com>
Date: Thu Dec 20 14:25:06 2018 +0800
2.txt add
commit 72a09cfaeaf9603303b59bfc90edefa5044e4b7d
Merge: 7df4a35 2e75e14
Author: mx <????@xx.com>
Date: Thu Dec 20 00:10:19 2018 +0800
2.复制上一个版本的commit-id
git reset --hard 8215c7f203024f7fee036f56dcf5b5bd4af0f232
Administrator@SKY-20180625ODQ MINGW64 /f/git/test (master)
$ git reset --hard 8215c7f203024f7fee036f56dcf5b5bd4af0f232
HEAD is now at 8215c7f 2.txt add
3.push到远端仓库
git push -f origin master
-f参数要加,意为强制push,不加会因为远端版本优于本地仓库版本之前冲突而报错。
Administrator@SKY-20180625ODQ MINGW64 /f/git/test (master)
$ git push -f origin master
Total 0 (delta 0), reused 0 (delta 0)
To github.com:y723109056/test.git
+ 0ce9992...8215c7f master -> master (forced update)
二. revert指令,利用HEAD指针
git revert 撤销 某次操作,此次操作之前和之后的commit和history都会保留,并且把这次撤销作为一次最新的提交
a) git revert HEAD 撤销前一次 commit
b) git revert HEAD^ 撤销前前一次 commit
c) git revert commit-id(比如:fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff)撤销指定的版本,撤销也会作为一次提交进行保存
就拿git revert commit-id举例
3.txt文件现在有三条提交记录 现在指针指向0ed0ff79e1fcecb58d49343c1af759810491cf41 (HEAD -> master, origin/master)
Administrator@SKY-20180625ODQ MINGW64 /f/git/test (master)
$ cat 3.txt
aaaa
bbb
ccc
Administrator@SKY-20180625ODQ MINGW64 /f/git/test (master)
$ git log 3.txt
commit 0ed0ff79e1fcecb58d49343c1af759810491cf41 (HEAD -> master, origin/master)
Author: ??? <xxx@qq.com>
Date: Fri Dec 21 17:57:20 2018 +0800
3.txt modify add ccc
commit 1265a3c832dad0a29ccb19a2244153a8b1e4319d
Author: ??? <xxx@qq.com>
Date: Fri Dec 21 17:56:19 2018 +0800
3.txt modify add bbb
commit 33bd88e8bc63c1c44789156f654015ab6fbdd1b0
Author: ??? <xxx@qq.com>
Date: Fri Dec 21 17:54:46 2018 +0800
add 3.txt
Administrator@SKY-20180625ODQ MINGW64 /f/git/test (master)
$ git revert 1265a3c832dad0a29ccb19a2244153a8b1e4319d