如何从git push -f恢复?

时间:2016-09-14 13:11:41

标签: git github git-push data-recovery

我在一个分支上做了git push -f,现在却失去了其他人的工作。

我能够在我的github中看到其他人的工作显示(显示在活动中),但不能在远程分支中显示(我推动了我的更改)。不幸的是我没有我的本地repo与远程同步。所以我本地没有其他人代码。有没有办法可以把它送到我当地并恢复?

3 个答案:

答案 0 :(得分:4)

在做任何其他事情之前,请执行以下3个步骤 - 备份您的本地仓库,告诉其他人备份他的,并备份现在的远程仓库。没什么好看的,只需按Ctrl + C,Ctrl + V即可。在整个项目中执行此操作,以便备份工作树。如果您无法直接访问远程服务器,git clone就足够了,但我更喜欢真正的目录副本。

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

由于你正在使用Github,你可能也会看到https://stackoverflow.com/a/35273807/492336。显然有一个Github的事件API可能很有用。

答案 1 :(得分:3)

您可以通过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:

$ 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

答案 2 :(得分:-6)

我不是git用户,但是几天前我发了一篇文章,我想这个问题也解决了你的问题。

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
相关问题