Git:恢复到以前的提交状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5004237/
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 10:06:10  来源:igfitidea点击:

Git: Revert to previous commit status

gitcommitreset

提问by MrBubbles

I'm confused. I want to go back to a previous commit that I identified in "git log".

我糊涂了。我想回到我在“git log”中确定的先前提交。

But when I do "git checkout ", I don't get said commit. Nothing changes. It tells me I'm in detached HEAD mode, but the files I want are not there.

但是当我做“git checkout”时,我没有得到所说的提交。没有什么改变。它告诉我我处于分离的 HEAD 模式,但我想要的文件不在那里。

What the eff am I doing wrong?

我做错了什么?

MrB

先生

采纳答案by Marc W

git reset --hard <commit>From the manpage:

git reset --hard <commit>从联机帮助页:

Matches the working tree and index to that of the tree being switched to. Any changes to tracked files in the working tree since are lost.

将工作树和索引与要切换到的树的索引相匹配。此后对工作树中跟踪文件的任何更改都将丢失。

git checkoutis for switching your working directory to a different branch or commit. This does not move the HEADthere.

git checkout用于将您的工作目录切换到不同的分支或提交。这不会移动HEAD那里。

回答by Adam Dymitruk

DO NOT git reset -hard it is PERMANENT!

不要 git reset -hard 它是永久性的!

Please use

请用

git stash -u 

instead! If you have a piece of work in there that you zapped by accident, you can still get it back. This never gets pushed to your remote unless you choose to do so by making a branch out of it and pushing it up.

反而!如果您在那里有一件不小心弄坏的作品,您仍然可以找回它。这永远不会被推送到您的遥控器,除非您选择通过创建一个分支并将其向上推。

Also, you are on the right track that you can use git checkoutto accomplish the same thing. The syntax is

此外,您走在了正确的轨道上,可以git checkout用来完成同样的事情。语法是

git checkout HEAD -- . 

But it has the same problem as git reset --hard. Stick with stash and you will save losing your hair in the future.

但它有同样的问题git reset --hard。坚持藏匿,你将来可以避免脱发。

Longer answer

更长的答案

The above solutions revert all your changes. However, you asked how to get rid of some of the changes. I'll add this for completeness.

上述解决方案会还原您的所有更改。但是,您询问如何摆脱某些更改。为了完整起见,我会添加这个。

To do this, you can

为此,您可以

git add file1 file2 file3
git stash save --patch

You will now be prompted for what exactly you want to make dissappear... down to what ever level of granularity. So you can safely "reject" only a few changes to a single file if you choose to do so.

您现在将被提示输入您想要消失的确切内容......细化到任何级别的粒度。因此,如果您选择这样做,您可以安全地“拒绝”对单个文件的少数更改。