git 如何撤消工作区中的某些更改并返回到上次提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11267074/
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
How I undo some changes in my workspace and get back to my last commit?
提问by Enrichman
Possible Duplicate:
Git: Revert to previous commit status
可能的重复:
Git:恢复到以前的提交状态
I've tried some changes in my code but I messed up too many things (never work when you're tired and it's late) and I just want to go back to my last commit. I DIDN'T do git add
or git commit
and obviously if I do git pull
everything is up to date.
我已经尝试对我的代码进行了一些更改,但是我搞砸了太多事情(当你累了的时候从不工作,而且很晚了),我只想回到我的最后一次提交。我没有做,git add
或者git commit
很明显,如果我做的git pull
一切都是最新的。
I thought that git checkout
was what I needed but it didn't worked.. any help?
我以为这git checkout
就是我所需要的,但它没有用..有什么帮助吗?
回答by Matt Enright
The answers mentioning reset --hard
will do what you want (as well as some other things that you may or may not), but you were correct in thinking that checkout
was the command you needed. The problem is that checkout
does two different things, so you need to supply it with a path argument:
提到的答案reset --hard
会做您想做的事情(以及您可能会或可能不会做的其他一些事情),但是您认为这checkout
是您需要的命令是正确的。问题是它checkout
做了两件不同的事情,所以你需要为它提供一个路径参数:
git checkout .
git checkout .
from the root of your repository will do you just fine.
从您的存储库的根目录就可以了。
回答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 checkout to accomplish the same thing. The syntax is 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 checkout 来完成同样的事情。语法是git checkout HEAD -- .
. 但它有同样的问题git reset --hard
。坚持藏匿,你将来可以避免脱发。
Longer answer
更长的答案
The above solutions revert all your changes. However, you asked how to get rid of someof 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.
您现在将被提示输入您想要消失的确切内容......细化到任何级别的粒度。因此,如果您选择这样做,您可以安全地“拒绝”对单个文件的少数更改。
回答by toske
git reset --hard
this will remove all of your untracked changes to last commit in repository.
git reset --hard
这将删除存储库中最后一次提交的所有未跟踪更改。
回答by William Pursell
git reset --hard
will discard all of your work, but you probably want to do:
git reset --hard
将丢弃您的所有工作,但您可能想要这样做:
git stash save "A bunch of stuff that didn't work"
on the off chance that you want to recover some of the changes.
如果您想恢复某些更改,则可能性很小。