简体   繁体   English

如何还原github仓库

[英]How to restore github repository

I deleted local git repository and remake local git repository. 我删除了本地git存储库,然后重新制作了本地git存储库。 after that I pushed one project into remote repository by using below command 之后,我使用以下命令将一个项目推送到远程存储库中

git push -f origin master

but when i checked my projects on Gihub, I found out that there is nothing except one project which i just pushed from local repository. 但是当我在Gihub上检查项目时,发现除了从本地存储库中推送的一个项目外,什么也没有。 all existing project disappear. 现有的所有项目都消失了。 it's like master branch is overwritten. 就像master分支被覆盖一样。 I know i should fetch first before doing 'push'. 我知道我应该先做“推”操作。 I did't even clone 我什至没有克隆
is there any solution to restore? 有什么解决方案可以还原吗? I want to turn it back to prior Gihub master version. 我想将其转回以前的Gihub主版本。

By using the -f flag, you told Git to overwrite any server-sided changes. 通过使用-f标志,您告诉Git覆盖服务器端的所有更改。

Because you are using GitHub, it is possible to retrieve the previous commit from GitHub. 因为您使用的是GitHub,所以可以从GitHub检索先前的提交。 I found this blog entry: 我找到此博客条目:

https://objectpartners.com/2014/02/11/recovering-a-commit-from-githubs-reflog/ https://objectpartners.com/2014/02/11/recovering-a-commit-from-githubs-reflog/

I would try this soon, I don't know how long GitHub saves stalled commits. 我会尽快尝试,不知道GitHub将停滞的提交保存多长时间。

You didn't simply pushed to master, you pushed with the -f flag ( --force ) which will bypass any conflict check. 您并没有简单地推向高手,而是使用-f标志( --force )进行推入,该标志将绕过任何冲突检查。 You should never force-push a branch unless you really know what you are doing . 除非您真的知道自己在做什么,否则切勿强行推分支。

The safe command to use to push your local master is (notice no -f flag): 用于推送本地主机的安全命令是(注意no -f标志):

git push origin master

Because GitHub doesn't expose backups, if you overridden a branch with a --force push there is no way to restore it. 因为GitHub不公开备份,所以如果使用--force push覆盖分支,则无法还原它。

Your only chance is to recover the deleted branch locally and, once recovered, push it again to GitHub. 您唯一的机会是在本地恢复已删除的分支 ,一旦恢复,将其再次推送到GitHub。 If you also deleted your repo locally, then there is no way to recover it. 如果您还在本地删除了您的存储库,则无法恢复它。

If you know the hash of the last commit on the prior github master version, you can locally do git reset --hard {hash} followed by git push -f origin master to force-push it back onto github, but this would only work if you still had a local copy of the git repository with all of the prior commits. 如果您知道先前的github主版本上的最后一次提交的哈希,则可以在本地执行git reset --hard {hash}然后执行git push -f origin master强制将其推回到github上,但这仅适用如果您仍然具有所有先前提交的git存储库的本地副本。

Otherwise, the -f argument to git push means to force-push despite any warnings. 否则, git push-f参数意味着即使有任何警告也要强制推送。 You should never do this unless you are completely sure of what you are doing. 除非您完全确定自己在做什么,否则永远不要这样做。

The only possible chance you may have of recovery is to try the following, if and only if you know the hash of the last commit on the prior github master. 恢复的唯一可能机会是,当且仅当您知道先前github主机上的最后一次提交的哈希时,才尝试以下操作。 In a web browser, navigate to: 在网络浏览器中,导航至:

https://github.com/{username}/{repository}/commit/{hash}

From there, you can use github's web UI to issue a pull request from that commit back into the master branch. 从那里,您可以使用github的Web UI发出来自该提交的拉取请求,并返回到master分支。

If you are on the same machine that you pushed you can try and use the git reflog to checkout the commit before you forced the push. 如果您与推送的计算机位于同一台计算机上,则可以在强制推送之前尝试使用git reflog检出提交。

reflog will contain all the changes that you have made to your repository locally reflog将包含您对本地存储库所做的所有更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM