git reset三个选项

  --mix,--hard,--soft

数据

  针对每个选项都是操作这个文件。

[root@centos demo]# git init
Initialized empty Git repository in /root/demo/.git/
[root@centos demo]# echo one >> a.txt
[root@centos demo]# git add a.txt
[root@centos demo]# git commit -m "first commit"
[master (root-commit) b7ee3a2] first commit
1 file changed, 1 insertion(+)
create mode 100644 a.txt
[root@centos demo]# echo two >> a.txt
[root@centos demo]# git commit -am "second commit"
[master 92ae9c4] second commit
1 file changed, 1 insertion(+)
[root@centos demo]# echo three >> a.txt
[root@centos demo]# git commit -am "third commit"
[master 0985eec] third commit
1 file changed, 1 insertion(+)
[root@centos demo]# echo four >> a.txt
[root@centos demo]# git commit -am "four commit"
[master 5bd480c] four commit
1 file changed, 1 insertion(+)
[root@centos demo]# git show-branch --more=4 #查看四次提交记录
[master] four commit
[master^] third commit
[master~2] second commit
[master~3] first commit

  

git reset --mix

  在省略reset选项的时候,默认的就是使用--mix

[root@centos demo]# git reset HEAD~2
Unstaged changes after reset:
M a.txt
[root@centos demo]# git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory) modified: a.txt no changes added to commit (use "git add" and/or "git commit -a")
[root@centos demo]# cat a.txt
one
two
three
four
[root@centos demo]# git diff
diff --git a/a.txt b/a.txt
index 69f75fc..a4c0ca1 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1,4 @@
one
two
+three
+four

  从运行结果可以看出来,--mix有以下特点:

    

  假设使用reset命令从版本D回到版本B,那么HEAD就会指向B。同时,版本D的代码内容并不会删除,会保留版本D相对于B的diff。

  可以使用git reset ORIG_HEAD回到之前的版本。

git reset --hard

  对原始数据进行下面操作:

[root@centos demo]# git show-branch --more=4
[master] four commit
[master^] third commit
[master~2] second commit
[master~3] first commit [root@centos demo]# git reset --hard HEAD~2
HEAD is now at e57a55f second commit [root@centos demo]# git show-branch --more=4
[master] second commit
[master^] first commit [root@centos demo]# git status
On branch master
nothing to commit, working tree clean [root@centos demo]# cat a.txt
one
two

  从运行结果可以发现,--hard选项有以下特点:

 

  使用--hard选项回到版本B的啥时候,HEAD会回到版本B,同时,代码内容也会回到版本B,也就是说,版本B之后的内容都被删除了。

  但是还是可以使用git reset ORIG_HEAD回到版本D。然后版本D的内容就会恢复,但是需要再次git add && git commit。

git reset --soft

[root@centos demo]# git show-branch --more=5
[master] four commit
[master^] third commit
[master~2] second commit
[master~3] first commit [root@centos demo]# git reset --soft HEAD~2 [root@centos demo]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage) modified: a.txt [root@centos demo]# git diff
#注意上面一个命令提示修改了a.txt,但是却没有diff [root@centos demo]# cat a.txt #a文件的内容仍旧没变
one
two
three
four [root@centos demo]# git reset ORIG_HEAD [root@centos demo]# git show-branch --more=5
[master] four commit
[master^] third commit
[master~2] second commit
[master~3] first commit

  --soft选项很特别,和--mix很相似,区别是:

  

  1、如果从D版本回到B版本,进行git diff时,不会显示版本D相对于版本B的diff,因为在B处执行git diff时,是和D进行diff,而不是和B进行diff(但是执行git status时,还是会提示有变化,但是可以忽略)

  2、在从B版本回到D版本后,不用在进行git add && git commit。可以认为HEAD一直都指向D,执行B的只是一个tmp_head。

  

git reset的用法的更多相关文章

  1. Git reset 常见用法

    Git reset 1. 文件从暂存区回退到工作区 2. 版本回退 1.1 git reset HEAD filename :回退文件,将文件从暂存区回退到工作区 //也可以使用 git reset ...

  2. 【Git 学习三】深入理解git reset 命令

    重置命令(git reset)是Git 最常用的命令之一,也是最危险最容易误用的命令.来看看git reset命令用法. --------------------------------------- ...

  3. git reset 与 git revert的区别?

    一,git reset的功能: 该命令修改HEAD的位置,即将HEAD指向的位置改变为之前存在的某个版本, 说明: 修改后,push到远程仓库时需要使用"git push -f"提 ...

  4. git revert用法以及与git reset的区别

    git revert用法 git revert 撤销 某次操作,此次操作之前和之后的commit和history都会保留,并且把这次撤销 作为一次最新的提交 * git revert HEAD     ...

  5. Git复习(十三)之git revert用法及与git reset区别

    git revert用法以及与git reset的区别 git revert用法 git revert 撤销 某次操作,此次操作之前和之后的commit和history都会保留,并且把这次撤销 作为一 ...

  6. git reset 版本回退的三种用法总结

    git reset (–mixed) HEAD~1 回退一个版本,且会将暂存区的内容和本地已提交的内容全部恢复到未暂存的状态,不影响原来本地文件(未提交的也不受影响) git reset –soft ...

  7. git reset用法

    git 删除 错误 提交的 commit 方法:         根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:    git reset -- ...

  8. 代码回滚:git reset、git checkout和git revert区别和联系

    git reset.git checkout和git revert是你的Git工具箱中最有用的一些命令.它们都用来撤销代码仓库中的某些更改,而前两个命令不仅可以作用于提交,还可以作用于特定文件. 因为 ...

  9. [译]git reset

    git reset 如果说git revert是一个安全的撤销方式, 那么git reset就是一个非常危险的方法了. 当你使用git reset撤销的时候, 你没有可能在回到最初了-他是一个永久的不 ...

随机推荐

  1. Kernel数据结构移植(list和rbtree)

    主要移植了内核中的 list,rbtree.使得这2个数据结构在用户态程序中也能使用. 同时用 cpputest 对移植后的代码进行了测试.(测试代码其实也是使用这2个数据结构的方法) 内核代码的如下 ...

  2. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  3. 获取WebApplicationContext的几种方式

    加载WebApplicationContext的方式 WebApplicationContext是ApplicationContext的子接口,纵观Spring框架的几种容器,BeanFactory作 ...

  4. [BZOJ 2759] 一个动态树好题

    [BZOJ 2759] 一个动态树好题 题目描述 首先这是个基环树. 然后根节点一定会连出去一条非树边.通过一个环就可以解除根的答案,然后其他节点的答案就可以由根解出来. 因为要修改\(p_i\),所 ...

  5. jsonp形式的ajax请求:

    sonp形式的ajax请求:并且通过get请求的方式传入参数,注意:跨域请求是只能是get请求不能使用post请求 <!DOCTYPE html> <html> <hea ...

  6. leetCode练习1

    代码主要采用C#书写 题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你 ...

  7. Mqtt用户认证

    http://emqtt.com/docs/v2/guide.html 1默认是匿名认证,不用输入用户名和密码,直接可连接 2如何开启用户名和密码认证模式 2-1关闭匿名认证 在你的MQTT安装目录下 ...

  8. (折扣计算)需求说明:普通顾客购物满100元打9折;会员购物打8折;会员购物满200元打7.5折(判断语句if-else和switch语句的嵌套结

    package com.summer.cn; import java.util.Scanner; /** * @author Summer *折扣计算 需求说明:普通顾客购物满100元打9折:会员购物 ...

  9. Java多线程(三)—— synchronized关键字详解

    一.多线程的同步 1.为什么要引入同步机制 在多线程环境中,可能会有两个甚至更多的线程试图同时访问一个有限的资源.必须对这种潜在资源冲突进行预防. 解决方法:在线程使用一个资源时为其加锁即可. 访问资 ...

  10. python:利用configparser模块读写配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...