恢复因Git reset –hard而丢失的文件

huangapple go评论70阅读模式
英文:

Regain the files lost due to Git reset --hard

问题

我有一个远程分支,似乎弄乱了。以下是按照时间顺序发生的一系列步骤的情况:

  • 我在上面做了一些提交,让我们简单地称之为我的最新提交为提交 #4
  • 我的一个队友推送了提交 #5,我不知道这一点
  • 我开始继续下一步的工作并推送了另一个提交。然而,在这样做之前,我应该先在本地拉取更改(这是我的第一个错误)
  • 就在我推送我的更改的时候,有一个提交 #6 包含了我的代码更改和一个额外的提交 #7 合并提交。提交 #7 撤消了我的同事在提交 #5 中执行的所有代码更改(我不知道为什么会发生这种情况;因为我的同事的提交在完全不同的文件集中)
  • 我对提交 #7 进行了撤消操作,但 Git 不允许(因为它是一个合并提交)。所以我对提交 #6 进行了 git 撤消,太好了!现在这是提交 #8,头指向的地方

所以在这一点上,我的提交历史看起来是这样的:

  • 提交 #8: 撤消提交 #6 <- HEAD
  • 提交 #7: 合并提交(撤消了我同事在提交 #5 中的更改)
  • 提交 #6: 我对提交 #4 的更改(因为我在我的代码更改之前错过了拉取)
  • 提交 #5: 我同事的更改
  • 提交 #4: 基本

现在,这就是我犯了一个大错的地方!

我陷入了清理 Git 历史中的混乱的贪念中。所以我执行了这些命令:

  • git reset --hard <提交 #5 的哈希>
  • git push -f

我期望 HEAD 指向提交 #5,从那里我可以重新开始

然而,现在我的提交历史看起来是这样的:
提交 #6: 我对提交 #4 的更改(因为我在我的代码更改之前错过了拉取)
提交 #4: 基本

提交 #5,我的同事的更改消失了! :'(

我完全理解git reset --hard是一个需要非常负责任地执行的命令

是否有希望恢复提交 #5 的更改?

  • 我与其他同事核对过,看看他们是否有提交 #5 的副本,这样至少我们有一个参考来复制这些更改。不幸的是,他们都没有在提交 #5 后拉取最新的更新

  • 撰写提交 #5 中更改的同事肯定会在他的本地仓库中有它,但他已经度过了一周的假期

  • 检查了官方的 Git 文档,但它说一旦执行了git reset --hard,就没有逆转的办法了

  • 检查了git reflog,如预期地,没有找到任何东西

英文:

I have a remote branch, that I seem to have messed up. Here is a sequence of steps that took place in the chronological order:

  • I had made a few commits on it, lets call my latest commit as Commit #4 for simplicity
  • One of my teammates pushed Commit #5, that I wasn't aware of
  • I started working on the next steps and pushed another commit. However, I should have pulled the changes on my local before doing so (My first mistake)
  • Sooner I pushed my changes there was a Commit #6 consisting my code changes and an additional Commit #7 the merge commit. This Commit #7 reverted all the code changes performed by my colleague in Commit #5 (I don't know why this happened; as my colleague's commits were in an entirely different set of files)
  • I made a git revert on Commit #7, which Git didn't allow (as it was a merge commit). So I made git revert on Commit #6 and voila, all good! This is Commit #8 now, where the head is pointed

So this is how my commit history looked like, at this point:

  • Commit #8: Revert Commit #6 <- HEAD
  • Commit #7: The Merge Commit (that reverted my colleague's changes done in Commit #5)
  • Commit #6: My changes over Commit #4 (since I missed pulling before my code changes)
  • Commit #5: My Colleague's changes
  • Commit #4: Base

Now, this is where I commited a BLUNDER!

I got into the greed of clearing up the mess in the Git History. So I executed these commands:

  • git reset --hard <hash for Commit #5>
  • git push -f

I expected the HEAD to point to Commit #5, from where I could start afresh

However, this is what my commit history looks like now:
Commit #6: My changes over Commit #4 (since I missed pulling before my code changes)
Commit #4: Base

Commit #5, my colleague's changes are GONE! :'(

I totally understand that git reset --hard is one command that needs to be executed with a lot of responsibility

Is there any hope of getting the Commit #5 changes back?

  • I checked with my other colleagues to see if they have a copy of Commit #5, so that atleast we have a reference to replicate the changes. Unfortunately none of them had pulled the latest updates after commit #5

  • The colleague who had authored the changes in Commit #5 will certainly have it on his local repo, but he has gone on a week long vacation

  • Checked official Git documentation, but it says there is no way of reversal once git reset --hard is executed

  • Checked git reflog, as expected, nothing found there

答案1

得分: 2

只要使用您的文件创建了提交,git 就不会从其存储中删除这些文件,除非您使用 git gc 命令来要求它这样做(现在不要这样做!)。

您确定在 git reflog 中没有任何记录吗?您绝对可以通过 reflog 返回至至少第 8 个提交,然后可以找到第 5 个提交。您还可以尝试运行 git fsck --lost-found 命令,它将列出存在于 git 存储中但不属于任何引用历史的提交。

英文:

As long as a commit was created with your files, git never deletes the files from its store unless you ask it to with git gc (don't do this now!).

Are you sure there's nothing on git reflog? You should definitely be able to get back to at least #8 with reflog, from which you can find #5. You can also try git fsck --lost-found which will list you any commits which exist in git's store but are not part of any ref's history.

huangapple
  • 本文由 发表于 2023年6月8日 23:43:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定