git基础教程(17)回退提交push到远端的提交--git revert 详解

Posted 奇妙之二进制

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git基础教程(17)回退提交push到远端的提交--git revert 详解相关的知识,希望对你有一定的参考价值。


工作中难免会遇到一些commit需要回退,可以用reset和revert。

reset是回溯到指定的commit版本,指定的commit版本之后的操作commit都重置了。revert是删除指定的commit操作的内容,指定的commit之前和之后commit操作都不受影响,与此同时这个操作也会作为一个commit进行提交。但对于已经提交到远端的公共分支比如master上的commit只能用revert来进行回退操作。

git revert使用挺简单的,但要理解其本质,其本质是对需要revert的提交生成逆向的新的提交
比如你touch 了reamde文件,然后
git add .
git commit -m “add readme”
git push

一顿操作下来,突然后悔提交了,那就git revert 需要回退的commit-id,相当做了以下操作:
rm readme
git add .
git commit -m " Revert “add readme”"

这是当前工作区域log的情况

bash-3.2$ git log
commit 6a43ed02f5831c9b91df9ae0dbfb9561d1ec2bb2
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:55:18 2017 +0800

    add e.go

commit 6a307a975fa676954c61078a3d786ef4ba084fd1
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:55:02 2017 +0800

    add d.go

commit 0bf71363290e8db926d442234eefb4cee2b3fdc8
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:54:41 2017 +0800

    add c.go

commit 6a2cc3dd8385004a5b9145630cb0b67498155c6b
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:54:23 2017 +0800

    add b.go

commit d672cd988198d3aec6faaccf0e39ab38e2304d19
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:53:00 2017 +0800

    add a.go

commit 2405994636350db65ec02adcbb3a6f356140b075
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:51:13 2017 +0800

    Create README.md

commit a432247e2acad106b7386d610df21ee1afbeaac6
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:51:03 2017 +0800

    Initial commit

1.比如你当前想回退某个commit

比如如下commit

commit d672cd988198d3aec6faaccf0e39ab38e2304d19
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:53:00 2017 +0800

    add a.go

方式一:bash-3.2$ git revert HEAD~4

用法: 当前commit为HEAD,然后每一个commit加一得到要撤销的commit 为HEAD~4

commit 6a43ed02f5831c9b91df9ae0dbfb9561d1ec2bb2  HEAD
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:55:18 2017 +0800

    add e.go

commit 6a307a975fa676954c61078a3d786ef4ba084fd1   HEAD~1
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:55:02 2017 +0800

    add d.go

commit 0bf71363290e8db926d442234eefb4cee2b3fdc8   HEAD~2
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:54:41 2017 +0800

    add c.go

commit 6a2cc3dd8385004a5b9145630cb0b67498155c6b   HEAD~3
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:54:23 2017 +0800

    add b.go

commit d672cd988198d3aec6faaccf0e39ab38e2304d19   HEAD~4
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:53:00 2017 +0800

    add a.go

方式二:bash-3.2$ git revert d672cd988198d3aec6faaccf0e39ab38e2304d19 或者 git revert d672cd9

用法: git revert + 完整的commit或者该commit前7位即可

bash-3.2$ git log
commit 3c149a450e2b86218a588423e116ee678acc2514
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 15:04:37 2017 +0800

    Revert "add a.go"

    This reverts commit d672cd988198d3aec6faaccf0e39ab38e2304d19.

commit 6a43ed02f5831c9b91df9ae0dbfb9561d1ec2bb2
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:55:18 2017 +0800

    add e.go

commit 6a307a975fa676954c61078a3d786ef4ba084fd1
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:55:02 2017 +0800

    add d.go

commit 0bf71363290e8db926d442234eefb4cee2b3fdc8
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:54:41 2017 +0800

    add c.go

commit 6a2cc3dd8385004a5b9145630cb0b67498155c6b
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:54:23 2017 +0800

    add b.go

commit d672cd988198d3aec6faaccf0e39ab38e2304d19
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:53:00 2017 +0800

    add a.go

2.回退连续的几个commit,这种情况比较常见

比如回退最新的三个commit:HEAD,HEAD~1,HEAD~2

commit 6a43ed02f5831c9b91df9ae0dbfb9561d1ec2bb2   HEAD
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:55:18 2017 +0800

    add e.go

commit 6a307a975fa676954c61078a3d786ef4ba084fd1   HEAD~1
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:55:02 2017 +0800

    add d.go

commit 0bf71363290e8db926d442234eefb4cee2b3fdc8   HEAD~2
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:54:41 2017 +0800

    add c.go

commit 6a2cc3dd8385004a5b9145630cb0b67498155c6b   HEAD~3
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:54:23 2017 +0800

    add b.go

commit d672cd988198d3aec6faaccf0e39ab38e2304d19   HEAD~4
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:53:00 2017 +0800

    add a.go
    
commit 2405994636350db65ec02adcbb3a6f356140b075   HEAD~5
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:51:13 2017 +0800

    Create README.md

commit a432247e2acad106b7386d610df21ee1afbeaac6   HEAD~6
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:51:03 2017 +0800

    Initial commit

比如最新的三个commit有问题需要回退 git revert X...Y

用法: X…Y 代表一个左开右闭区间(X,Y],不包括X,包括Y. 其中Y为起点commit,X为终点commit的下一个commit

方式一:
bash-3.2$:git revert HEAD~3...HEAD

方式二:
git revert 6a2cc3d...6a43ed0

或者

git revert 6a2cc3dd8385004a5b9145630cb0b67498155c6b...6a43ed02f5831c9b91df9ae0dbfb9561d1ec2bb2

结果如下:

bash-3.2$ git log
commit af4076c5fe336d877ec36d1580c8b153400981c4
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 15:45:43 2017 +0800

    Revert "add c.go"

    This reverts commit 0bf71363290e8db926d442234eefb4cee2b3fdc8.

commit 9cee5e52f1b4e5a2c3fe8e697c5dda6a844fe923
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 15:45:40 2017 +0800

    Revert "add d.go"

    This reverts commit 6a307a975fa676954c61078a3d786ef4ba084fd1.

commit a75fce274472132cf59f24a7bfb11a8c47295987
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 15:45:38 2017 +0800

    Revert "add e.go"

    This reverts commit 6a43ed02f5831c9b91df9ae0dbfb9561d1ec2bb2.

commit 6a43ed02f5831c9b91df9ae0dbfb9561d1ec2bb2
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:55:18 2017 +0800

    add e.go

commit 6a307a975fa676954c61078a3d786ef4ba084fd1
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:55:02 2017 +0800

    add d.go

commit 0bf71363290e8db926d442234eefb4cee2b3fdc8
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:54:41 2017 +0800

    add c.go

commit 6a2cc3dd8385004a5b9145630cb0b67498155c6b
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:54:23 2017 +0800

    add b.go

commit d672cd988198d3aec6faaccf0e39ab38e2304d19
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:53:00 2017 +0800

    add a.go

commit 2405994636350db65ec02adcbb3a6f356140b075
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:51:13 2017 +0800

    Create README.md

commit a432247e2acad106b7386d610df21ee1afbeaac6
Author: asturelizhe <asturelizhe@163.com>
Date:   Sun Apr 23 14:51:03 2017 +0800

    Initial commit

以上是关于git基础教程(17)回退提交push到远端的提交--git revert 详解的主要内容,如果未能解决你的问题,请参考以下文章

修改已push到远端的commit

git 的一些小 tips

git提交代码

Git&GitHub-基础教程

git 如何撤销已push的代码

Git-撤销(回退)已经add,commit或push的提交