回滚到最后一次 git 提交

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

Rollback to last git commit

git

提问by Blankman

I just did a

我刚做了一个

git commit -m "blah"

then I added some files, how do I rollback and remove what is in my current files that have not yet been added/committed?

然后我添加了一些文件,如何回滚并删除当前文件中尚未添加/提交的内容?

回答by Joe Hanink

Caveat Emptor- Destructive commands ahead.

警告 Emptor-前方的破坏性命令。

Mitigation- git reflogcan save you if you need it.

缓解-git reflog如果您需要,可以为您节省。



1) UNDOlocal file changes and KEEPyour last commit

1)撤销本地文件更改并保留上次提交

git reset --hard

2) UNDOlocal file changes and REMOVEyour last commit

2)撤销本地文件更改并删除您的最后一次提交

git reset --hard HEAD^

3) KEEPlocal file changes and REMOVEyour last commit

3)保留本地文件更改并删除您的最后一次提交

git reset --soft HEAD^

回答by Paul Pladijs

If you want to remove newly added contents and files which are already staged (so added to the index) then you use:

如果要删除已暂存的新添加的内容和文件(因此添加到索引中),则使用:

git reset --hard

If you want to remove also your latest commit (is the one with the message "blah") then better to use:

如果您还想删除最新的提交(带有“blah”消息的提交),那么最好使用:

git reset --hard HEAD^

To remove the untracked files (so new files not yet added to the index) and folders use:

要删除未跟踪的文件(因此新文件尚未添加到索引中)和文件夹,请使用:

git clean --force -d

回答by SpliFF

git reset --hardwill force the working directory back to the last commit and delete new/changed files.

git reset --hard将强制工作目录回到上次提交并删除新的/更改的文件。

回答by Sebastian Oliva

You can revert a commit using git revert HEAD^for reverting to the next-to-last commit. You can also specify the commit to revert using the id instead of HEAD^

您可以使用git revert HEAD^用于恢复到倒数第二次提交来恢复提交。您还可以使用 id 而不是 HEAD^ 指定要还原的提交

回答by Abhishek saharn

If you want to just uncommit the last commit use this:

如果您只想取消提交最后一次提交,请使用以下命令:

git reset HEAD~

work like charm for me.

工作对我来说很有魅力。

回答by Terje Norderhaug

An easy foolproof wayto UNDO local file changes since the last commit is to place them in a new branch:

自上次提交以来撤销本地文件更改的一种简单万无一失的方法是将它们放在新分支中:

git branch changes
git checkout changes
git add .
git commit

This leaves the changes in the new branch. Return to the original branch to find it back to the last commit:

这将更改留在新分支中。回到原来的分支,找回上次提交:

git checkout master

The new branch is a good place to practice different ways to revert changes without risk of messing up the original branch.

新分支是练习不同方法来恢复更改的好地方,而不会有弄乱原始分支的风险。