git reset soft,hard,mixed区别详解

最新文章转移至 https://www.xingjiehu.com/2019/03/06/git-reset命令详解/
GIT reset命令,似乎让人很迷惑,以至于误解,误用。但是事实上不应该如此难以理解,只要你理解到这个命令究竟在干什么。

首先我们来看几个术语

HEAD
这是当前分支版本顶端的别名,也就是在当前分支你最近的一个提交,也就是本地仓库,即你的commit记录

Index
index也被称为staging area,即add的记录

Working Copy
working copy代表你正在工作的那个文件

先执行git log查看当前的记录,这里是我预先做了几次commit

commit fd9b11185a41cf5cfe187be61dc43c06dd7b5420
Author: shooke <xingjiehu@163.com>
Date:   Fri Feb 24 15:23:55 2017 +0800

    3

commit 0aa75d4355211a8b1943d1c8d5b4904e18053bcf
Author: shooke <xingjiehu@163.com>
Date:   Fri Feb 24 15:23:37 2017 +0800

    2

commit 2bdd42d126d269688be33759f96e4015a85e720b
Author: shooke <xingjiehu@163.com>
Date:   Fri Feb 24 15:23:04 2017 +0800

    1

当前工作区是干净的,下面执行以下命令

##1 --soft

shooke@shooke-pc:/var/www/gittest$ git status
位于分支 master
无文件要提交,干净的工作区
shooke@shooke-pc:/var/www/gittest$ git reset --soft HEAD~1
shooke@shooke-pc:/var/www/gittest$ git log
commit 0aa75d4355211a8b1943d1c8d5b4904e18053bcf
Author: shooke <xingjiehu@163.com>
Date:   Fri Feb 24 15:23:37 2017 +0800

    2

commit 2bdd42d126d269688be33759f96e4015a85e720b
Author: shooke <xingjiehu@163.com>
Date:   Fri Feb 24 15:23:04 2017 +0800

    1
shooke@shooke-pc:/var/www/gittest$ git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	修改:     a.txt


我们发现使用git reset --soft后我们的commit少了一条,a.txt是已经add过的状态
也就是说–soft只是撤销了commit。**但是a.txt的内容是没有改变的。**我们用公式标示一下

HEAD != index = Working Copy
只撤销了commit ,保留了index(add过)和工作区

–mixed

我们恢复到3条记录的状态,继续看下面的操作

shooke@shooke-pc:/var/www/gittest$ git reset --mixed HEAD~1
重置后取消暂存的变更:
M	a.txt
shooke@shooke-pc:/var/www/gittest$ git log
commit 0aa75d4355211a8b1943d1c8d5b4904e18053bcf
Author: shooke <xingjiehu@163.com>
Date:   Fri Feb 24 15:23:37 2017 +0800

    2

commit 2bdd42d126d269688be33759f96e4015a85e720b
Author: shooke <xingjiehu@163.com>
Date:   Fri Feb 24 15:23:04 2017 +0800

    1
shooke@shooke-pc:/var/www/gittest$ git status
位于分支 master
尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:     a.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

我们发现git提示我们需要add,也就是说暂存区发生了修改,但是a.txt 的内容没有改变
我们再用公式标示一下

HEAD = index != Working Copy
撤销了commit 、index,工作区不变

–hard

我们恢复到3条记录的状态,继续看下面的操作

shooke@shooke-pc:/var/www/gittest$ git reset --hard HEAD~1
HEAD 现在位于 0aa75d4 2
shooke@shooke-pc:/var/www/gittest$ git log
commit 0aa75d4355211a8b1943d1c8d5b4904e18053bcf
Author: shooke <xingjiehu@163.com>
Date:   Fri Feb 24 15:23:37 2017 +0800

    2

commit 2bdd42d126d269688be33759f96e4015a85e720b
Author: shooke <xingjiehu@163.com>
Date:   Fri Feb 24 15:23:04 2017 +0800

    1
shooke@shooke-pc:/var/www/gittest$ git status
位于分支 master
无文件要提交,干净的工作区

这一次没有a.txt有修改。也就是说工作区的文件与暂存去、本地仓库是一致的。打开a.txt发现里面的内容变成了2,也就是说a.txt内容也跟着回退到了上次commit时的代码。用公式表示

HEAD = index = Working Copy
commit 、index和工作区文件都回退改变

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值