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

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

问题描述

我想拆分一次提交,但不确定要使用哪个重置选项.

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

我正在查看页面 简单的英语,什么是git reset"?,但我意识到我并不真正理解 git 索引或暂存区是什么,因此解释没有帮助.

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

此外,在我看来,--mixed--soft 的用例在那个答案中是一样的(当你想修复和重新提交时).有人可以进一步分解吗?我意识到 --mixed 可能是一个选项,但我想知道 为什么.最后,--hard 怎么样?

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?

谁能给我一个工作流程示例,说明如何选择 3 个选项?

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

推荐答案

当您修改存储库中的文件时,更改最初是取消暂存的.为了提交它,你必须使用 git add 暂存它——即将它添加到索引中.提交时,提交的更改是已添加到索引中的更改.

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 至少会更改当前分支 (HEAD) 指向的位置.--mixed--soft 的区别在于你的索引是否也被修改了.所以,如果我们在分支 master 上进行这一系列的提交:

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指向C,索引匹配C.

当我们运行 git reset --soft B 时,master(因此 HEAD)现在指向 B,但索引仍然有 C 的变化;git status 会将它们显示为暂存状态.因此,如果我们此时运行 git commit,我们将获得与 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)

现在让我们做git reset --mixed B.(注意:--mixed 是默认选项).再次,masterHEAD 指向B,但这次索引也被修改以匹配B.如果我们此时运行 git commit,则不会发生任何事情,因为索引与 HEAD 匹配.我们仍然在工作目录中有更改,但由于它们不在索引中,git status 将它们显示为未暂存.要提交它们,您可以 git add 然后像往常一样提交.

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.

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

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.

最后,一个可视化:

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

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