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

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

  • How do I revert a Git repository to a previous commit? 如何将Git存储库还原为先前的提交? 41 answers 41个答案

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" 

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

  1. git reset --hard 恢复

    git reset --hard ,再然后,悲剧上演~ 恢复方法: 使用 git reflog 来找到最近提交的信息,这里贴出部分信息: F:\voidy>git reflog WARNING: ...

  2. git reset --hard HEAD 后恢复暂存区文件

    git reset --hard HEAD 后恢复暂存区文件 1.参考资料 git reset–hard后恢复暂存区文件 git reset–hard后恢复暂存区(未提交)文件 关于git reset ...

  3. git reset 怎么还原_git 本地修改被reset后怎么恢复

    reset命令有3种方式: 1:git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息 2:gi ...

  4. git head指向老版本_16. branch与HEAD的理解,使用git reset恢复到过去的某个版本

    1. branch指针与HEAD指针 1.1 branch是什么? branch本质上是指向commit对象的指针. 1.2 HEAD是什么? HEAD是指向branch的指针,指向的是当前所在的分支 ...

  5. git reset --mixed, - soft和--hard有什么区别?

    我希望拆分提交,不知道要使用哪个重置选项. 我在看页面你能解释一下"git reset"用简单的英语做什么吗? ,但我意识到我并不真正理解git索引或临时区域是什么,因此解释没有帮 ...

  6. a commit git 参数是什么意思_Git中的git reset的三种参数的区别

    我们平时在使用git的时候,经常会遇到需要撤销上次操作的需求,这时候需要用到git reset的这个命令,他的使用就是 "git-reset – Reset current HEAD to ...

  7. git reset中hard与soft区别

    前言  对于我们日常开发中,我们常用的方法可能是git reset --hard,来强行回退commit所提交的信息,这是不友好的,也许我们希望对误操作提交的commit不进行删除而是重新修改并进行提 ...

  8. git reset和revert

    reset: 如果想恢复到之前某个提交的版本,且那个版本之后提交的版本我们都不要了,就可以用这种方法. 1.找到自己要修改的分支输入git log查看历史版本号 例如:deab40为最新版本 deab ...

  9. git reset, git checkout, git revert 区别 (译)

    博客原文地址: http://blog.mexiqq.com/index.php/archives/3/ 题记:团队中大多数成员使用 sourceTree 和 github 两款 git 工具,然而大 ...

最新文章

  1. 运维企业部分学习开篇
  2. 如何编译 Linux 内核
  3. mysql分析表增删改统计_MySQL增删改查|附思维导图
  4. 怎样用命令行生成自己的签名文件keystore
  5. python入门-简单的文件备份程序
  6. 如何将html转为report,如何把Html5 Report Viewer添加到Web项目
  7. 算法21----重塑矩阵 LeetCode566
  8. Dubbo源码解析之SPI(一):扩展类的加载过程
  9. 用Python下载抖音无水印视频
  10. MYS-6ULX-IOT 开发板测评——实现简单的物联网应用
  11. 宁皓网ninghao.net这个人的教程不错
  12. Go项目配置管理神器之viper使用详解
  13. 大数据与区块链的爱恨情仇,一场技术界相爱相杀的爱恋!
  14. 如何记录日常收支,将账目导出生成表格保存
  15. 桌面文件突然不见了怎么恢复?
  16. 为什么要学习科学的方法论
  17. 使用MATLAB的residue()命令求传递函数的展开式
  18. http://sishuok.com/forum/blog/index.html?search_keywords=%E7%A0%94%E7%A3%A8%E8%AE%BE%E8%AE%A1%E6%A8%
  19. yox.js 的使用
  20. Web设计网站软件推荐

热门文章

  1. Binder跨进程通信原理(二):内存映射mmap原理分析
  2. Android输入输出系统之TouchEvent流程
  3. android 工信部偷跑流量
  4. Android draw bitmap 图片不显示的问题
  5. Android 让View 和 ViewGroup 同时响应点击或者长按事件
  6. 如何在TortoiseGit中使用ssh-keygen生成的key
  7. 第十三周项目-交通工具类程序
  8. hibernate tools for eclipse plugins在线怎么安装
  9. 手机编程micropython_MicroPython可视化拼插编辑器:让硬件编程更智能!
  10. python简单界面实现-python实现的简单窗口倒计时界面实例