具体请参考文档:
Git Revert | Atlassian Git Tutorial
Resetting, Checking Out & Reverting | Atlassian Git Tutorial
git revert并不会删除要改回的commit的之后所有的历史commit, 而reset必须要删除目标commit之后所有的commit(也不是真正删除了,只是让之后的那些commit变成orphan commits,然后在git做垃圾回收时会被真正删除掉)。
另外,git revert也并不会删除目标commit, 而是添加了一个新的commit,这个新的commit跟目标commit执行的是相反的动作,所以合到一起之后,就相当于undo了之前的目标commit.
D:\project\ideaProject\git-branch-test>echo "add file1" >file1
D:\project\ideaProject\git-branch-test>git add .
D:\project\ideaProject\git-branch-test>git commit -m "add file1"
[new_fea2 8ed5c03] add file1
1 file changed, 1 insertion(+)
create mode 100644 file1
D:\project\ideaProject\git-branch-test>echo "first modification" >>file1
D:\project\ideaProject\git-branch-test>git add .
D:\project\ideaProject\git-branch-test>git commit -m "first mofication to file1"
[new_fea2 c350525] first mofication to file1
1 file changed, 1 insertion(+)
D:\project\ideaProject\git-branch-test>echo "second modification" >> file1
D:\project\ideaProject\git-branch-test>git add .
D:\project\ideaProject\git-branch-test>git commit -m "second mofication to file1"
[new_fea2 8eac363] second mofication to file1
1 file changed, 1 insertion(+)
D:\project\ideaProject\git-branch-test>type file1
"add file1"
"first modification"
"second modification"
D:\project\ideaProject\git-branch-test>
D:\project\ideaProject\git-branch-test>git log --oneline
8eac363 (HEAD -> new_fea2) second mofication to file1
c350525 first mofication to file1
8ed5c03 add file1
3e0ce1e (origin/release, origin/hotfix, origin/feature) Initial commit
D:\project\ideaProject\git-branch-test>git reset --hard 3e0ce1e
Revert "second mofication to file1"
This reverts commit 8eac36338e231f250d40b3ce33b0b3494e8ee60f.
D:\project\ideaProject\git-branch-test>git log --oneline
0f97133 (HEAD -> new_fea2) Revert "second mofication to file1"
8eac363 second mofication to file1
c350525 first mofication to file1
8ed5c03 add file1
3e0ce1e (origin/release, origin/hotfix, origin/feature) Initial commit
D:\project\ideaProject\git-branch-test>type file1
"add file1"
"first modification"