“git checkout -f”和“git reset --hard HEAD”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6201877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:03:54  来源:igfitidea点击:

What is difference between "git checkout -f" and "git reset --hard HEAD"?

git

提问by osm

I need to revert local changes for deployments. (I'd used svn revertfor this in old skool SVN days.)

我需要恢复部署的本地更改。(我svn revert在旧的 skool SVN 时代使用过这个。)

And im using git reset --hard HEADfor this. (Also git fetchand git merge origin/$branch --no-fffor syncronizing with upstream branch.)

我正在git reset --hard HEAD为此使用。(此外git fetchgit merge origin/$branch --no-ff用于与上游分支同步处理资料。)

But some articles points git checkout -ffor reverting changes.

但是有些文章指出git checkout -f要恢复更改。

What's key differences between this commands. Which way is the recommended?

此命令之间的主要区别是什么。推荐哪种方式?

采纳答案by Colin Hebert

The two of them have the exact same effect. I recommend you to choose the solution you're the more comfortable with.

两者的效果完全一样。我建议您选择更适合您的解决方案。

But if in this particular case the effect is the same, with different values it would be completely different. Basically (there is more, see linked topics) with a reset you move the current branch and the HEAD to a specific commit but with a checkout, you only move the HEAD . For more details see below.

但是如果在这种特殊情况下效果是相同的,不同的值就会完全不同。基本上(还有更多,请参阅链接的主题)通过重置将当前分支和 HEAD 移动到特定提交,但通过结帐,您只移动 HEAD 。有关更多详细信息,请参见下文。



Resources:

资源:

On the same topic:

关于同一主题:

回答by user1807768

Don't have the rep to comment on other answers yet, I just wanted to add that I came across a case where the two commands do NOT have the same effect. I got into a weird state so this is definitely an edge case. Here is what happened:

还没有代表对其他答案发表评论,我只是想补充一点,我遇到了两个命令没有相同效果的情况。我进入了一种奇怪的状态,所以这绝对是一个边缘情况。这是发生的事情:

I was in a branch, everything clean. I checked out master git checkout masterand found from git statusthat there were changes to existing files not staged for a commit(yes, on the code I just checked out). I tried stashing to go back to a clean state, the stash claimed to have completed but git statusstill was unchanged. Also tried git reset --hard HEAD. It too reported successfully completing yet the status was no different. I could not abort these weird changes.

我在一个分支,一切都很干净。我检查了 mastergit checkout master并发现git status对现有文件的更改没有为提交而暂存(是的,在我刚刚检查的代码上)。我尝试藏匿以恢复到干净状态,藏匿声称已完成但git status仍未改变。也试过了git reset --hard HEAD。它也报告成功完成,但状态没有什么不同。我无法中止这些奇怪的变化。

However, git checkout -fsolved this. I was able to get away from this strange state. So, in at least some ways, the two are not the same.

不过,git checkout -f解决了这个问题。我能够摆脱这种奇怪的状态。因此,至少在某些方面,两者并不相同。