简体   繁体   English

如何将工作阶段恢复到git中的最后一次提交?

[英]How to revert working stage to last commit in git?

I have edited some files in working stage (not added them with git add -A . no commitment). 我已经在工作阶段编辑了一些文件(没有用git add -A添加它们。没有承诺)。

Now I would like to revert the changes back to the last commit in my local branch. 现在,我想将更改恢复到本地分支中的最后一次提交。

What is to do? 怎么办?

I already searched a bit and found: 我已经搜索了一下,发现:

git rebase -i HEAD

but then I get: 但是我得到:

Cannot rebase: You have unstaged changes.
Please commit or stash them.

So what is the right way? 那么正确的方法是什么?

You can throw away all your uncommitted changes with: 您可以使用以下方法丢弃所有未提交的更改:

git reset --hard

WARNING: this really will get rid of all your changes that are staged or just in your working tree and go back to the state of the last commit, so use it with care. 警告:这实际上将消除您已暂存的所有更改或仅在工作树中进行的所有更改,并返回到最后一次提交的状态,因此请谨慎使用。

If you might want your local changes back again, and just want to get them out of the way temporarily (eg to work on something else) you could just do: 如果您可能希望再次返回本地更改,并且只是想暂时避免它们(例如,从事其他工作),则可以这样做:

git stash

... instead ...代替

With

git stash

you can store your currently working state to the stash and will have access to the files later on. 您可以将当前的工作状态存储到存储中,以后可以访问文件。 After you "stashed", you should automatically be at your last commit. “隐藏”之后,您应该自动处于最后一次提交。

Now you're into that I'll strongly recommend to give a read to this article, and get it understood forever. 现在,您将强烈建议阅读本文,并使其永远被理解。 http://git-scm.com/2011/07/11/reset.html http://git-scm.com/2011/07/11/reset.html

if you dont want keep those changes then 如果您不想保留这些更改,那么

git reset --hard

or if you want to keep them 或者如果您想保留它们

git reset --soft

if you want to save so you can use them letter on then 如果您想保存,以便可以在其上使用字母

git stash

to apply those latter on 将后者应用于

git stash --apply stash@{number}

Just do the command below. 只需执行以下命令。 It will remove your changes 它将删除您的更改

git reset HEAD .
git checkout .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM