happenask
happenask

Reputation: 1084

How to restore github repository

I deleted local git repository and remake local git repository. 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. all existing project disappear. it's like master branch is overwritten. 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.

Upvotes: 1

Views: 7906

Answers (4)

CodeWizard
CodeWizard

Reputation: 142532

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.

reflog will contain all the changes that you have made to your repository locally

Upvotes: 0

Elias Holzmann
Elias Holzmann

Reputation: 3679

By using the -f flag, you told Git to overwrite any server-sided changes.

Because you are using GitHub, it is possible to retrieve the previous commit from GitHub. I found this blog entry:

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.

Upvotes: 1

mkrufky
mkrufky

Reputation: 3388

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.

Otherwise, the -f argument to git push means to force-push despite any warnings. 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. 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.

Upvotes: 0

Simone Carletti
Simone Carletti

Reputation: 176552

You didn't simply pushed to master, you pushed with the -f flag (--force) which will bypass any conflict check. 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):

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.

Your only chance is to recover the deleted branch locally and, once recovered, push it again to GitHub. If you also deleted your repo locally, then there is no way to recover it.

Upvotes: 0

Related Questions