简体   繁体   中英

Is there any way to restore a GitHub repo that has been deleted with the code and commit history saved locally?

Essentially I was on a team that had a GitHub repository that is now deleted. I still have the code saved locally on my computer and would like to put the project on my personal GitHub. I would like there to be some way to have the commit history still intact so that the other team members can have credit for the work they did. However, since the repo is gone I can no longer fork it to do this. Is there any way to do this or should I just recreate the repo and credit them in the README? Or just leave it off entirely?

First of, the commit history is stored in the .git folder of each copy of the repo. It is not stored or managed by Github, rather Github's copy of the repo also has a .git folder that contains the same commit history as your local copy. Basically, as long as that .git folder is " intact ", then the commit history is still going to be " intact " when you push the repo somewhere, anywhere else.

Second, Github does support recovering or restoring a deleted repo, see Restoring a deleted repository from the Github docs. But that option depends on if you owned the account or organization that created the old repo, and if the old repo wasn't deleted for more than 90 days, and if that old repo has no forks. If you can restore it, then you can fork it to your personal account.

If restoring the old repo is not an option, the other (simpler, IMHO) option is to create a new repo and push your local copy to that new remote. When you say you " have the code saved locally on my computer ", I am assuming it is still a working git repo like this:

$ cd myproject
$ git log
$ git remote -v
origin  git@github.com:ginomempin/my-old-repo.git (fetch)
origin  git@github.com:ginomempin/my-old-repo.git (push)

where my-old-repo.git is the deleted Github repo and the logs still show all the commits. Go to Github and create an entirely new repo. When it redirects you to the "your new repository is empty" page, notice the instructions on pushing an existing repo:

在此处输入图像描述

Because you already have an origin in your local copy, you can instead change it to the new URL:

$ git remote set-url origin git@github.com:ginomempin/my-new-repo.git
$ git remote -v
origin  git@github.com:ginomempin/my-new-repo.git (fetch)
origin  git@github.com:ginomempin/my-new-repo.git (push)

Now simply push it all to the new repo:

$ git push --all

That new repo will now contain all the old commits, as if it was just like the old one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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