【问题标题】:How to recover last commit after git hard reset?git硬重置后如何恢复上次提交?
【发布时间】:2013-11-22 19:04:09
【问题描述】:

我可以在硬重置后恢复以下提交吗?

步骤:

1) $ (master) // ....made a bunch of changes to files thinking I was working on a branch
2) $ git checkout -b 001-branch  // copy changes to a branch to work with
3) $ (001-branch) // make some more changes to files
4) $ (001-branch) git commit -a -m 'added and changed stuff'
// at this point I was just going to pull force master to latest then rebase my 001-branch off of original master (not the stuff I had modified)
5) $ (001-branch) git checkout master
6) $ (master) git reset --hard HEAD
7) $ (master) git pull
8) $ (master) git checkout 001-branch // go back to my branch and rebase my changes
9) $ (001-branch) // oops...my changes were all kiboshed and I don't see the commit I did per git lg

有什么办法可以摆脱这种混乱来恢复我的更改?

【问题讨论】:

  • 几个问题:(1)第4步的提交真的成功了吗? (2) 你真的在第 8 步做了一个变基吗? (3)“所有kiboshed”是什么意思?你有没有看到第 4 步中的提交,它是否以某种方式被破坏了,或者它只是丢失了? (4) 你在 001-branch 的git reflog 中看到你的提交了吗? reflog 是让自己摆脱这种事情的常用方法。
  • 这能回答你的问题吗? Recover from git reset --hard?

标签: git


【解决方案1】:

要查看对 001-branch 所做的所有更改,您可以使用git reflog 001-branch,但是,您可能认为您在 001-branch 中做了什么,也许您做了另一个分支,所以您可能需要查看所有更改为git reflog

【讨论】:

    【解决方案2】:

    老实说,我不明白发生了什么。

    git reflog 打印分支指向的提交内容。 您可以使用它来查找最近 local 提交的 SHA1 总和。

    【讨论】:

      【解决方案3】:

      每次 git 做一些剧烈的事情,比如改变或倒带分支,它都会在 reflog 中记录。换句话说,仔细检查git reflog 的输出,它会告诉你分支曾经有过的所有转换。然后您可以使用git show commit_id 进行检查并使用git checkout commit_id 进行返回。

      【讨论】:

        【解决方案4】:

        git reflog show 是否打印丢失提交的哈希?如果是,那么git checkout -b recovery-branch commitId 将创建一个指向丢失提交的新分支。然后可以根据需要合并。

        【讨论】:

          猜你喜欢
          • 2019-10-05
          • 2021-11-19
          • 2018-11-10
          • 2017-12-16
          • 1970-01-01
          • 2018-10-01
          • 1970-01-01
          • 2011-07-12
          • 2015-06-03
          相关资源
          最近更新 更多