`

(转载)git revert和git reset的区别及示例

阅读更多

原文地址:http://blog.csdn.net/hudashi/article/details/7664460

 

原文如下:

git revert 是撤销某次操作,此次操作之前的commit都会被保留
git reset 是撤销某次提交,但是此次之后的修改都会被退回到暂存区
具体一个例子,假设有三个commit, git st:
commit3: add test3.c
commit2: add test2.c
commit1: add test1.c
当执行git revert HEAD~1时, commit2被撤销了
git log可以看到:
commit1:add test1.c
commit3:add test3.c
git status 没有任何变化
如果换做执行git reset --soft(默认) HEAD~1后,运行git log
commit2: add test2.c
commit1: add test1.c
运行git status, 则test3.c处于暂存区,准备提交。
如果换做执行git reset --hard HEAD~1后,
显示:HEAD is now at commit2,运行git log
commit2: add test2.c
commit1: add test1.c
运行git st, 没有任何变化
另外:
git revert <commit log string>是撤消该commit,作为一个新的commit。
 
本人示例如下:

1,初始化git仓库

mkdir test
cd test
git init
touch test1.c
touch test2.c
touch test3.c
git add test1.c
git commit -m “第一次提交”
git add test2.c
git commit -m “第二次提交”
git add test3.c
git commit -m “第三次提交”

 

git log查看结果:

commit 4f6aa621149f98edf8887fa2219b63c84a95f317
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:42:26 2016 +0800

    “第三次提交”

commit 4bf0d2237bddd50e3674d264eebcf696af61790a
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:42:26 2016 +0800

    “第二次提交”

commit 61f08c6d73f7f5822a39fba5b177d5394c77f26d
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:42:26 2016 +0800

    “第一次提交”

2,执行git revert HEAD~1

查看git log

 

结果:

commit 8119ed3e329a2cd939291949cd265db6fc658b76
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:48:31 2016 +0800

    Revert "“第二次提交”"

    This reverts commit 4bf0d2237bddd50e3674d264eebcf696af61790a.

commit 4f6aa621149f98edf8887fa2219b63c84a95f317
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:42:26 2016 +0800

    “第三次提交”

commit 4bf0d2237bddd50e3674d264eebcf696af61790a
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:42:26 2016 +0800

    “第二次提交”

commit 61f08c6d73f7f5822a39fba5b177d5394c77f26d
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:42:26 2016 +0800

    “第一次提交”

 git status查看:

On branch master
nothing to commit, working directory clean

 ls查看:

test1.c	test3.c

发现 test2.c文件找不到了,但是可以通过再次运行git revert HEAD~1命令找回来,找回来的文件放在了暂存区里了。

 

3,重新建立删除原有test仓库,并重新建立和初始化

执行git reset HEAD~1

git log查看:

commit 09e51cd049ee37f1fece19e042390196367328f2
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:56:29 2016 +0800

    “第二次提交”

commit 988e3312b3c6e5e3a20d3dc95cfa9b61e558b5fc
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:56:29 2016 +0800

    “第一次提交”

 git status查看结果:

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	test3.c

nothing added to commit but untracked files present (use "git add" to track)

 

test3.c文件在当前工作目录,并没有进入暂存区,所以是红色字体显示(未被追踪的文件)

 

4,重新建立删除原有test仓库,并重新建立和初始化

执行git reset --soft HEAD~1

git log查看:

 

commit 09e51cd049ee37f1fece19e042390196367328f2
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:56:29 2016 +0800

    “第二次提交”

commit 988e3312b3c6e5e3a20d3dc95cfa9b61e558b5fc
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:56:29 2016 +0800

    “第一次提交”

 git status查看结果:

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   test3.c

 test3.c文件放在了暂存区里了。

无论是git reset HEAD~1 还是 git reset --soft HEAD~1,都能撤销指定的提交,只不过,前者将撤销的提交放在了当前目录,后者将撤销的提交放在了暂存区。而且使用ls查看,三个文件也都在当前工作目录里呢。

 

注意:如果执行git reset --soft HEAD~2则第二次提交和第三次提交的文件都撤销了,放入暂存区里了。

 

5,重新建立删除原有test仓库,并重新建立和初始化

执行git reset --hard HEAD~1结果:

HEAD is now at 09e51cd “第二次提交”

git log查看:

commit 09e51cd049ee37f1fece19e042390196367328f2
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:56:29 2016 +0800

    “第二次提交”

commit 988e3312b3c6e5e3a20d3dc95cfa9b61e558b5fc
Author: zhanghaiwei <forest@163.com>
Date:   Tue Feb 16 11:56:29 2016 +0800

    “第一次提交”

 git status查看:

On branch master
nothing to commit, working directory clean

ls查看:

test1.c	test2.c

 发现test3 文件丢失。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics