Whats the difference between git reset --mixed, --soft, and --hard?
https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard
When you modify a file in your repository, the change is initially unstaged. In order to commit it, you must stage it—that is, add it to the index—using git add
. When you make a commit, the changes that are committed are those that have been added to the index.
git reset
changes, at minimum, where the current branch (HEAD
) is pointing. The difference between --mixed
and --soft
is whether or not your index is also modified. So, if we're on branch master
with this series of commits:
- A - B - C (master)
HEAD
points to C
and the index matches C
.
When we run git reset --soft B
, master
(and thus HEAD
) now points to B
, but the index still has the changes from C
; git status
will show them as staged. So if we run git commit
at this point, we'll get a new commit with the same changes as C
.
Okay, so starting from here again:
- A - B - C (master)
Now let's do git reset --mixed B
. (Note: --mixed
is the default option). Once again, master
and HEAD
point to B, but this time the index is also modified to match B
. If we run git commit
at this point, nothing will happen since the index matches HEAD
. We still have the changes in the working directory, but since they're not in the index, git status
shows them as unstaged. To commit them, you would git add
and then commit as usual.
And finally, --hard
is the same as --mixed
(it changes your HEAD
and index), except that --hard
also modifies your working directory. If we're at C
and run git reset --hard B
, then the changes added in C
, as well as any uncommitted changes you have, will be removed, and the files in your working copy will match commit B
. Since you can permanently lose changes this way, you should always run git status
before doing a hard reset to make sure your working directory is clean or that you're okay with losing your uncommitted changes.
总结:
假设有A<--B<--C 三个commit,当前分支master指向C【先确保本地是clean的状态】
执行git reset --soft B,本地文件变成修改状态,并且是B到C的变化。
执行 git reset --mixed B,master指向B,并且文件work directory的文件内容会切换到B所在的状态
想到了关于soft的使用,如果在回滚之后,进行commit。其实就相当于一次squash
最新文章
- angular学习笔记(二十八)-$http(6)-使用ngResource模块构建RESTful架构
- Jquery判断变量是否为空
- IE6 IE7 ‘JSON’ 未定义
- js实现web网页版台球游戏
- 一段关于测试和自定义Attribute的代码
- xamarin UWP平台下 HUD 自定义弹窗
- [BZOJ2038]小Z的袜子(莫队算法)
- jboss4.2.3建立oracle JMS应用
- Ubuntu下Code::Blocks无法编译 /bin/sh: 1: g++ not found 解决办法
- ECSHOP报错误Deprecated: preg_replace(): The /e modifier is depr
- div+css3列布局,带详尽注释
- Java学习----Java程序结构
- Android(java)学习笔记253:ContentProvider使用之内容观察者02
- HDU4532(组合DP)
- 购物车CheckBox全选、反选
- java_web学习(2)Servlet
- 201521123039《java程序设计》第五周学习总结
- UITextFiled的输入框改成一条下划线
- poj2029 Get Many Persimmon Trees
- How to preview html file in our browser at sublime text?
热门文章
- 有趣的console
- BZOJ 2741 L (可持久化01Trie+分块)
- CF1029E Tree with Small Distances (贪心)
- Springboot - -web应用开发-Servlets, Filters, listeners
- iOS基础UI控件介绍-Swift版
- 题解 洛谷 P3381 【【模板】最小费用最大流】
- Java集合源代码剖析(一)【集合框架概述、ArrayList、LinkedList、Vector】
- php通过shell调用Hadoop的方法
- 【C#】报表制作&;lt;机房重构&;gt;
- IIS访问站点,出现connection refused