如何使用'git reset --hard HEAD'恢复到之前的提交? [重复]

本文翻译自:How do I use 'git reset --hard HEAD' to revert to a previous commit? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

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? 我在做什么是对还是错?


#1楼

参考:https://stackoom.com/question/dywo/如何使用-git-reset-hard-HEAD-恢复到之前的提交-重复


#2楼

WARNING: git clean -f will remove untracked files, meaning they're gone for good since they aren't stored in the repository. 警告: git clean -f将删除未跟踪的文件,这意味着它们已经消失,因为它们没有存储在存储库中。 Make sure you really want to remove all untracked files before doing this. 在执行此操作之前,请确保您确实要删除所有未跟踪的文件。


Try this and see git clean -f . 试试这个,看看git clean -f

git reset --hard will not remove untracked files, where as git-clean will 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 -xdf CAUTION! git clean -xdf 小心! This will also delete ignored files 这也将删除被忽略的文件

#3楼

First, it's always worth noting that git reset --hard is a potentially dangerous command, since it throws away all your uncommitted changes. 首先,值得注意的是git reset --hard是一个潜在的危险命令,因为它会丢弃所有未提交的更改。 For safety, you should always check that the output of git status is clean (that is, empty) before using it. 为安全起见,在使用之前,应始终检查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. Git仅在您使用git add (或使用git add )或创建提交时记录文件的状态。 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. 一旦你创建了一个让你的项目文件处于特定状态的提交,它们就非常安全,但是在那之前Git并没有真正“跟踪你的文件的变化”。 (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.) (例如,即使您执行git addgit 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 当我想恢复到之前的提交时,我使用:git reset --hard HEAD并且git返回:HEAD现在位于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: 如果你执行git reset --hard <SOME-COMMIT>那么Git会:

  • Make your current branch (typically master ) back to point at <SOME-COMMIT> . 使当前分支(通常为master )返回<SOME-COMMIT>
  • Then make the files in your working tree and the index ("staging area") the same as the versions committed in <SOME-COMMIT> . 然后使工作树中的文件和索引(“临时区域”)与<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. HEAD指向您当前的分支(或当前提交),因此所有git reset --hard HEAD将执行的操作是丢弃您拥有的任何未提交的更改。

So, suppose the good commit that you want to go back to is f414f31 . 所以,假设您要返回的好提交是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: (您可以通过git log或任何历史记录浏览器找到它。)然后根据您的具体操作,您可以选择几个不同的选项:

  • Change your current branch to point to the older commit instead. 将当前分支更改为指向旧提交。 You could do that with git reset --hard f414f31 . 你可以用git reset --hard f414f31做到这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. 此外,您在f414f31之后f414f31的提交将不再位于master分支的历史记录中。
  • 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. 创建一个新的提交,它表示与f414f31完全相同的项目状态,但只是将其添加到历史记录中,因此您不会丢失任何历史记录。 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" 
发布了0 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105364435
今日推荐