如何从 git push -f 中恢复?

How to recover from git push -f?

我在一个分支上完成了 git push -f,现在丢掉了其他人的工作。

我可以在我的 github 中看到显示了其他人的工作(显示在 activity 中),但没有显示在远程分支中(我推送更改的地方)。不幸的是,我的本地仓库没有与远程同步。所以我本地没有其他人的代码。有什么办法可以把它送到我的本地并恢复吗?

我不是 git 用户,但几天前我遇到了这个 post,我想这个也能解决你的问题。

http://ohshitgit.com/

# undo the last commit, but leave the changes available
git reset HEAD~ --soft
git stash
# move to the correct branch
git checkout name-of-the-correct-branch
git stash pop
git add . # or add individual files
git commit -m "your message here"
# now your changes are on the correct branch

在执行任何其他操作之前,请执行以下 3 个步骤 - 备份您的本地存储库,告诉其他人备份他的,并按现在的样子备份远程存储库。没什么特别的,只需 Ctrl+C、Ctrl+V 即可。在您的整个项目上执行此操作,以便您也备份工作树。如果您不能直接访问远程服务器,git clone 就足够了,但我更喜欢真实的目录副本。

完成后,其他人可以使用 git push -f 重新上传他的分支。如果他有新的提交,你将不得不弄清楚他最后推送了哪个提交,但我相当确定不会丢失任何实际提交。如果其他人已经完成 git pull -f,即使这样他也可以使用 git reflog 来恢复他的本地分支。

由于您使用的是 Github,您可能还会看到 。显然有一个 Github 的事件 API 可能会有用。

您可以通过 REST-API

访问 GitHub 的 reflog

这将向您显示事件:

$ curl https://api.github.com/repos/<user>/<repo>/events

找到损坏的提交后,只需添加对它的引用即可:

POST /repos/<user>/<repo>/git/refs
{
  "ref": "refs/heads/featureA",
  "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd"
}

或卷曲:

$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" \
    -X POST -d '{"ref":"refs/heads/featureA", \
                 "sha":"aa218f56b14c9653891f9e74264a383fa43fefbd"}' \
    https://api.github.com/repos/<user>/<repo>/git/refs