我如何使用'git reset --hard HEAD'恢复到之前的提交? [英] How do I use 'git reset --hard HEAD' to revert to a previous commit?

查看:638
本文介绍了我如何使用'git reset --hard HEAD'恢复到之前的提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Git会跟踪我对应用程序所做的更改,并且它会保留这些更改,直到我提交更改,但这里是我挂起的位置:

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

  git reset --hard HEAD  



Git返回:

  HEAD现在处于820f417 micro 

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



我的下一步是:

  git add。 
git commit -mrevert

但是我的硬盘上没有任何文件发生变化驱动器...



我在做什么对/错?

解决方案

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

最初你会说以下内容:


因此,我知道Git会跟踪我对应用程序所做的更改,并且它会保留它们直到我提交了更改,但这里是我挂断的地方:

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



在您的问题中,您继续询问以下内容:


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



然后我如何恢复硬盘上的文件回到那个以前的提交?


如果您执行 git reset --hard< SOME-COMMIT> 然后Git会:


  • 让你当前的分支(通常 master )回到< SOME-COMMIT>

  • 然后在工作树和索引中创建文件staging area)与< SOME-COMMIT> 中提交的版本相同。



HEAD 指向您当前的分支(或当前提交),因此,所有 git reset --hard HEAD 将会做的就是抛弃所有未提交的修改。

,假设你想返回的好提交是 f414f31 。 (你可以通过 git log 或任何历史浏览器找到)。然后根据你想要做什么,你有几个不同的选择:




  • 改为将当前分支更改为指向较旧的提交。你可以用 git reset --hard f414f31 来做到这一点。但是,这是重写分支的历史记录,因此如果您与任何人共享此分支,则应该避免它。此外,您在 f414f31 之后执行的提交将不再位于 master 分支的历史记录中。

  • 创建一个与 f414f31 完全相同的项目状态的新提交,但只是将其添加到历史记录中,所以您不要失去任何历史。您可以使用此答案中建议的步骤执行此操作 - 例如:

      git reset --hard f414f31 
    git reset --soft HEAD @ {1}
    git commit -m恢复项目状态f414f31



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:

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?

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?

解决方案

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

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:

In case this reveals a mistaken assumption, I should say that this isn't correct. 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 add to stage a new version of the file, that overwrites the previously staged version of that file in the staging area.)

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?

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

  • 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>.

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

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

  • 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 f414f31 will no longer be in the history of your master branch.
  • 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 HEAD'恢复到之前的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆