如何从意外的git push -f中恢复?


13

我只是跑git push -f错了,因此覆盖了一个远程分支。

原版的:

(remote origin:)
    branch master -> commit aaaaaaa
    branch foo    -> commit bbbbbbb

(local)
    branch master -> commit ccccccc
    branch foo    -> commit ddddddd

之后git push -f:

(remote origin:)
    branch master -> commit ccccccc
    branch foo    -> commit ddddddd

在本地存储库中,我正在处理master分支,因此我可以将分支还原master为commit aaaaaaa,因为我可以aaaaaaa从获取分支git reflog。但是,我无法提交,bbbbbbb因为我之前没有拉过git push -f。

我已经git reflog在远程存储库中尝试过,但是在裸存储库中的reflog中没有什么用。

如何还原分支foo以提交bbbbbbb到远程存储库中?

(PS我不知道的实际值bbbbbbb。)


但是远程仓库是一个裸仓库。
— 谢耶莱2011年

Answers:


12

尝试这个:

  1. 通过SSH连接到远程。

  2. 对整个远程存储库进行备份。

    tar cvzf project-backup.tgz /path/to/project.git
    
  3. 如果您至少知道的前几个字符bbbbbbb,请使用git show bbbbbb和/或git log bbbbbb查找完整的提交哈希。(如果只需要哈希,git rev-parse bbbbbb也可以使用,但是检查总是更好。)

    如果你不知道该值在所有的运行git fsck,你应该得到“晃来晃去提交”名单。使用git show <hash>和检查每个提交,git log <hash>直到找到正确的提交为止。

  4. 更新分支引用:

    echo aaaaaaaaaaaaaaa.... > refs/heads/master
    echo bbbbbbbbbbbbbbb.... > refs/heads/foo
    
  5. 使用git log master和git log foo确保您恢复了正确的分支。


谢谢,我用保存了悬挂的分支git fsck。
— 谢耶利

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.