$ git log --pretty=oneline
f7c9eb4120d368915a6e5491bf85dc5cd87a424f (HEAD -> master) Revert "change"
9eecd39b8e6a2109c3678c42dc034db2190840f6 (origin/master, origin/HEAD) change
e4f5e6920c22b748d765824e0be21015332fafa4 init
我们在最新版本的基础上,把最后一次提交的修改给回退了,同时用回退后版本生成了一次提交,并生成了新的 commit id,这样所有的操作 log 均得到了正确的保留。
注意:命令 revert 是指回退指定版本的修改内容,而不是指指定版本到当前版本的所有内容,所以如果给定的 commit id 不是当前版本的最新 commit id,那么就会报错:
$ git log --pretty=oneline
45734f3dc12f45e78504bd0fb52ba334c0ae2c9f (HEAD -> master, origin/master, origin/HEAD) test
7c82b777a40dfb24fc1bba404be0daa3b3923a95 test revert
f7c9eb4120d368915a6e5491bf85dc5cd87a424f Revert "change"
9eecd39b8e6a2109c3678c42dc034db2190840f6 change
e4f5e6920c22b748d765824e0be21015332fafa4 init
$ git revert 7c82b
error: could not revert 7c82b77... test revert
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
You are currently reverting commit 7c82b77.
(fix conflicts and run "git revert --continue")
(use "git revert --abort" to cancel the revert operation)
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
如果上图,我们跳过了 45734 的修改,想只回退 7c82b 的修改,这时候就提示冲突了,这时候要么手工修改冲突,要么取消 revert 后,一次指定多个 revert id 来实现,下面是一次使用多个 revert id 进行实现:
$ git revert --abort
$ git log --pretty=oneline
45734f3dc12f45e78504bd0fb52ba334c0ae2c9f (HEAD -> master, origin/master, origin/HEAD) test
7c82b777a40dfb24fc1bba404be0daa3b3923a95 test revert
f7c9eb4120d368915a6e5491bf85dc5cd87a424f Revert "change"
9eecd39b8e6a2109c3678c42dc034db2190840f6 change
e4f5e6920c22b748d765824e0be21015332fafa4 init
$ git revert 45734 7c82b
[master 8140eb0] Revert "test"
1 file changed, 1 insertion(+), 3 deletions(-)
[master 191816d] Revert "test revert"
1 file changed, 1 insertion(+), 2 deletions(-)
$ git log --pretty=oneline
191816d84c02a4ba591d3739739ddd0df114d7f1 (HEAD -> master) Revert "test revert"
8140eb0f69493f1a6306b0e92822f07c049a7a50 Revert "test"
45734f3dc12f45e78504bd0fb52ba334c0ae2c9f (origin/master, origin/HEAD) test
7c82b777a40dfb24fc1bba404be0daa3b3923a95 test revert
f7c9eb4120d368915a6e5491bf85dc5cd87a424f Revert "change"
9eecd39b8e6a2109c3678c42dc034db2190840f6 change
e4f5e6920c22b748d765824e0be21015332fafa4 init
这时候如果我们查看文件,会发现 45734 和 7c82b 的内容都被回退了,并新生成了两次的 commit(每个 commit id 的回退会单独生成一条记录)。
特别说明一下,为了保证 revert 的效果,建议每次提交修改的时候,尽量减少非耦合文件的一起提交,分批提交可以更利于以后的 revert。
将Git版本号编译进程序 https://www.linuxidc.com/Linux/2018-07/153239.htm
Git的工作模式和工作流程 https://www.linuxidc.com/Linux/2018-05/152154.htm
Git建立远程/本地服务器和Git命令的使用 https://www.linuxidc.com/Linux/2018-05/152153.htm
代码版本控制Git工具使用详解 https://www.linuxidc.com/Linux/2018-04/151973.htm
Git重要概念与常用命令 https://www.linuxidc.com/Linux/2018-04/151810.htm
Git常用命令总结 https://www.linuxidc.com/Linux/2018-04/151809.htm
Git常用命令整理,详细全面 https://www.linuxidc.com/Linux/2018-04/151805.htm
Git实用技巧和命令 https://www.linuxidc.com/Linux/2018-08/153480.htm
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx