“git reset --hard hash”有没有区别?和“git checkout hash”? [英] Is there a difference between "git reset --hard hash" and "git checkout hash"?

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

问题描述

虽然 reset checkout 在大多数情况下有不同用法,但我看不出有什么区别在这两者之间。

可能会有人或者没人会加入一个 - hard 选项来做某事基本的结帐可以。



也许有区别就是你会看到历史记录? >解答方案

这个答案主要是从我对前一个问题的回答中引用的: / 2530060 / can-you-explain-to-me-git-reset-in-plain-english> git reset in plain english



两个是非常不同的。它们会导致索引和工作树处于相同状态,但生成的历史记录和当前分支不同。



假设您的历史记录如下所示, master分支目前已经签出:

   -  A  -  B  -  C(HEAD,master)

,然后运行 git reset --hard B 。你会得到这个:

   -  A  -  B(HEAD,master)# -  C仍然存在,但没有
#分支指向它

如果您使用 - mixed 或 - soft - 唯一的区别是您的工作树和索引会发生什么变化。在 - hard 情况下,工作树和索引匹配 B



现在,假设您运行 git checkout B 代替。你会得到这个:

   -  A  -  B(HEAD) -  C(master)

你已经结束了分离的HEAD状态。 HEAD ,工作树,索引全部匹配 B ,与硬重置相同,但主分支被留下在 C 。如果你在这时做了一个新的提交 D ,你会得到这个,这可能不是你想要的:

   -  A  -  B  -  C(主)
\
D(HEAD)

因此,您可以使用checkout检查该提交。你可以摆弄它,做你喜欢的事情,但是你把你的分支留下了。如果您希望分支也移动,您可以使用重设。


While reset and checkout have different usages most of the time, I can't see what difference there is between these two.

There probably is one or nobody would have bothered adding a --hard option to do something the basic checkout can do.

Maybe there is a difference is the way you will see the history?

解决方案

This answer is mostly quoted from my answer to a previous question: git reset in plain english.

The two are very different. They result in the same state for your index and work tree, but the resulting history and current branch aren't the same.

Suppose your history looks like this, with the master branch currently checked out:

- A - B - C (HEAD, master)

and you run git reset --hard B. You'll get this:

- A - B (HEAD, master)      # - C is still here, but there's no
                            # branch pointing to it anymore

You'd actually get that effect if you use --mixed or --soft too - the only difference is what happens to your work tree and index. In the --hard case, the work tree and index match B.

Now, suppose you'd run git checkout B instead. You'd get this:

- A - B (HEAD) - C (master)

You've ended up in a detached HEAD state. HEAD, work tree, index all match B, same as with the hard reset, but the master branch was left behind at C. If you make a new commit D at this point, you'll get this, which is probably not what you want:

- A - B - C (master)
       \
        D (HEAD)

So, you use checkout to, well, check out that commit. You can fiddle with it, do what you like, but you've left your branch behind. If you want the branch moved too, you use reset.

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

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