如何使用 'git reset --hard HEAD' 恢复到以前的提交?

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

How do I use 'git reset --hard HEAD' to revert to a previous commit?

githeadgit-resetgit-revert

提问by Brian McDonough

I know that Git tracks changes I make to my application, and it holds on to them until I commit the changes, but here's where I'm hung up:

我知道 Git 会跟踪我对应用程序所做的更改,并且在我提交更改之前它会保留这些更改,但这里是我挂断的地方:

When I want to revert to a previous commit I use:

当我想恢复到以前的提交时,我使用:

git reset --hard HEAD

And Git returns:

Git 返回:

HEAD is now at 820f417 micro

How do I then revert the files on my hard drive back to that previous commit?

然后我如何将硬盘驱动器上的文件恢复到之前的提交?

My next steps were:

我接下来的步骤是:

git add .
git commit -m "revert"

But none of the files have changed on my hard drive...

但是我硬盘上的文件都没有改变......

What am I doing right/wrong?

我在做什么对/错?

回答by Mark Longair

First, it's always worth noting that git reset --hardis a potentially dangerous command, since it throws away all your uncommitted changes. For safety, you should always check that the output of git statusis clean (that is, empty) before using it.

首先,始终值得注意的是,这git reset --hard是一个潜在的危险命令,因为它会丢弃您所有未提交的更改。为了安全起见,git status在使用之前,您应该始终检查输出是否干净(即为空)。

Initially you say the following:

最初你说以下:

So I know that Git tracks changes I make to my application, and it holds on to them until I commit the changes, but here's where I'm hung up:

所以我知道 Git 会跟踪我对我的应用程序所做的更改,并且在我提交更改之前它会保留这些更改,但这是我挂断的地方:

That's incorrect. Git only records the state of the files when you stage them (with git add) or when you create a commit. Once you've created a commit which has your project files in a particular state, they're very safe, but until then Git's not really "tracking changes" to your files. (for example, even if you do git addto stage a new version of the file, that overwrites the previously staged version of that file in the staging area.)

那是不正确的。Git 仅在您暂存文件(使用git add)或创建提交时记录文件的状态。一旦你创建了一个让你的项目文件处于特定状态的提交,它们就非常安全,但在那之前 Git 并没有真正“跟踪更改”你的文件。(例如,即使您执行git add暂存文件的新版本,也会覆盖暂存区域中该文件的先前暂存版本。)

In your question you then go on to ask the following:

在你的问题中,你继续问以下问题:

When I want to revert to a previous commit I use: git reset --hard HEAD And git returns: HEAD is now at 820f417 micro

How do I then revert the files on my hard drive back to that previous commit?

当我想恢复到以前的提交时,我使用: git reset --hard HEAD 并且 git 返回: HEAD 现在在 820f417 micro

然后我如何将硬盘驱动器上的文件恢复到之前的提交?

If you do git reset --hard <SOME-COMMIT>then Git will:

如果你这样做,git reset --hard <SOME-COMMIT>那么 Git 将:

  • Make your current branch (typically master) back to point at <SOME-COMMIT>.
  • Then make the files in your working tree and the index ("staging area") the same as the versions committed in <SOME-COMMIT>.
  • 使您当前的分支(通常master)回到指向<SOME-COMMIT>.
  • 然后使工作树中的文件和索引(“暂存区”)与<SOME-COMMIT>.

HEADpoints to your current branch (or current commit), so all that git reset --hard HEADwill do is to throw away any uncommitted changes you have.

HEAD指向您当前的分支(或当前提交),因此所有git reset --hard HEAD要做的就是丢弃您拥有的任何未提交的更改。

So, suppose the good commit that you want to go back to is f414f31. (You can find that via git logor any history browser.) You then have a few different options depending on exactly what you want to do:

因此,假设您想要返回的良好提交是f414f31. (您可以通过git log或任何历史浏览器找到它。)然后您有几个不同的选项,具体取决于您想要做什么:

  • Change your current branch to point to the older commit instead. You could do that with git reset --hard f414f31. However, this is rewriting the history of your branch, so you should avoid it if you've shared this branch with anyone. Also, the commits you did after f414f31will no longer be in the history of your masterbranch.
  • Create a new commit that represents exactly the same state of the project as f414f31, but just adds that on to the history, so you don't lose any history. You can do that using the steps suggested in this answer- something like:

    git reset --hard f414f31
    git reset --soft HEAD@{1}
    git commit -m "Reverting to the state of the project at f414f31"
    
  • 更改当前分支以指向较旧的提交。你可以用git reset --hard f414f31. 但是,这是重写分支的历史记录,因此如果您已与任何人共享此分支,则应避免这样做。此外,您之后所做的提交f414f31将不再存在于您的master分支的历史记录中。
  • 创建一个与 完全相同的项目状态的新提交f414f31,但只是将其添加到历史记录中,因此您不会丢失任何历史记录。您可以使用此答案中建议的步骤来做到这一点- 例如:

    git reset --hard f414f31
    git reset --soft HEAD@{1}
    git commit -m "Reverting to the state of the project at f414f31"
    

回答by uday

WARNING: git clean -fwill remove untracked files, meaning they're gone for good since they aren't stored in the repository. Make sure you really want to remove all untracked files before doing this.

警告:git clean -f将删除未跟踪的文件,这意味着它们将永远消失,因为它们没有存储在存储库中。在执行此操作之前,请确保您确实要删除所有未跟踪的文件。



Try this and see git clean -f.

试试这个,看看git clean -f

git reset --hardwill not remove untracked files, where as git-cleanwill remove any files from the tracked root directory that are not under Git tracking.

git reset --hard不会删除未跟踪的文件,git-clean从跟踪的根目录中删除任何不在 Git 跟踪下的文件。

Alternatively, as @Paul Betts said, you can do this (beware though - that removes all ignored files too)

或者,正如@Paul Betts 所说,你可以这样做(但要注意 - 这也会删除所有被忽略的文件)

  • git clean -df
  • git clean -xdfCAUTION! This will also delete ignored files
  • git clean -df
  • git clean -xdf警告!这也将删除被忽略的文件