git revert vs git reset

具体请参考文档:

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"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值