git reset --mixed,--soft和--hard有什么区别? [英] What's the difference between git reset --mixed, --soft, and --hard?

查看:1172
本文介绍了git reset --mixed,--soft和--hard有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在寻找页面

stackoverflow.com/questions/2530060/can-you-explain-to-me-git-reset-in-plain-english\">你可以解释一下git reset用纯英语?,但我意识到我真的不明白git索引或暂存区是什么,因此解释没有帮助。



同样,对于 - mixed - soft 的用例在这个答案中看起来是一样的(当你想要修复并重新提交。)有人可以把它分解得更多吗?我意识到 - 混合可能是选择,但我想知道 。最后, - hard



有人可以给我一个工作流示例,说明如何选择3个选项发生?

解决方案

当您修改存储库中的文件时,更改最初是不分配的。为了提交它,你必须使用 git add 将其添加到索引中。提交时,所提交的更改是已添加到索引中的更改。



git reset 至少改变当前分支( HEAD )指向的位置。 - mixed - soft 之间的区别在于您的索引是否也被修改过。因此,如果我们在分支 master 上进行了这一系列的提交:

   -  A  -  B  -  C(master)

HEAD 指向 C ,索引匹配 C



当我们运行 git reset --soft B master (因此 HEAD )现在指向 B ,但该索引仍然具有从 C ; git status 会将它们显示为已暂存。因此,如果我们现在运行 git commit ,我们将得到一个新的提交,它的变化与 C 相同。






好的,所以从这里开始:

<$ p $现在让我们来看看


$ b $ code> git reset --mixed B 。 (注意: - mixed 是默认选项)。再次, master HEAD 指向B,但是这次索引也被修改为匹配 B 。如果我们此时运行 git commit ,那么自从索引匹配 HEAD 之后什么都不会发生。我们仍然在工作目录中进行了更改,但由于它们不在索引中,所以 git status 将它们显示为非暂存。要提交它们,你需要 git add 然后照常提交。






最后, - hard - mixed 相同(它会更改您的 HEAD 和index),除了 - hard 也修改你的工作目录。如果我们位于 C 并运行 git reset --hard B ,那么添加的变化 C 以及任何未提交的更改将被删除,并且工作副本中的文件将与commit B 匹配。由于您可以通过这种方式永久丢失更改,因此在执行硬重置之前,您应该始终运行 git status ,以确保您的工作目录清洁,或者您可以丢失未提交的变更。






最后,可视化:


I am looking to split a commit up and not sure which reset option to use.

I was looking at the page Can you explain what "git reset" does in plain english?, but I realized I don't really understand what the git index or staging area is and thus the explanations didn't help.

Also the use cases for --mixed and --soft look the same to me in that answer (when you want to fix and recommit.) Can someone break it down even more? I realize --mixed is probably the option to go with, but I want to know why. Lastly, what about --hard?

Can someone give me a workflow example of how selecting the 3 options would happen?

解决方案

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)

HEADpoints 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.


And finally, a visualization:

这篇关于git reset --mixed,--soft和--hard有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆