I forked a project, created a new branch from master, made changes and created a pull request to the original project. But suddenly I forgot I created this pull request and deleted my fork completely from remote (GitHub) and from my PC as well. How can I recreate a branch (or a fork) from a pull request in order to add a change and let it merge?
-
Is the PR still open? Afair if you delete the branch in your fork, the PR is automatically closed. Not sure what happens if you delete the fork instead. – Vampire Oct 05 '16 at 14:29
-
Still open, just with note: "from **me/branch deleted** to **project/master**" – aleskva Oct 05 '16 at 14:30
-
*exactly "to **project:master** from **unknown repository**" – aleskva Oct 05 '16 at 14:53
-
Does this answer your question? [Recover a commit sent as a pull-request from a deleted fork on GitHub](https://stackoverflow.com/questions/20977530/recover-a-commit-sent-as-a-pull-request-from-a-deleted-fork-on-github) – Inigo Dec 17 '21 at 08:25
2 Answers
There are two things you can do:
1. Contact GitHub support
While trying out my solution for you I deleted a fork where I myself had a PR still pending and had the same situation than you.
There is currently no way to reattach to that PR, besides contacting GitHub support. They can restore the deleted fork which will also reattach it to the pending PR. Then you can simply clone your fork, change your PR branch and push.
It was a matter of minutes in my case until GitHub staff responded to the contact form. applauding to GitHub
2. Make a new PR
If you don't want to bother GitHub support or they are too slow for you or are unwilling, you can do the following:
- recreate your fork
- recreate your branch from the pull request by doing
git fetch <your configured remote for upstream> refs/pull/<your PR number>/head:<your branchname>
This will recreate the PR branch for you locally, then change whatever you want to change, close the original PR and open a new one.

- 35,631
- 4
- 76
- 102
-
git fetch gives me `fatal: 'upstream' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.` – aleskva Oct 05 '16 at 15:04
-
of course you have to fill in how the remote is named in your local repository. For me I follow the wide-spread convention that my fork is called `origin` and the master repo is called `upstream`. – Vampire Oct 05 '16 at 15:06
-
I finally managed to do it using the 2nd way (fetch the code using your answer and creating a new PR), thank you – aleskva Oct 05 '16 at 16:03
One way to fix this:
$ mkdir repo
$ git init; git remote add origin git@github.com:original/repo.git # not your fork
Now open up .git/config
and add this line:
fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
so it reads:
[remote "origin"]
url = git@github.com:original/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
Then run
$ git fetch origin
$ git checkout origin/pr/<your pr number, as shown in their github repo>
Then just add a new remote to your fork, and push up the branch

- 28,221
- 15
- 85
- 110
-
`git fetch origin` produces this: `Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.` – aleskva Oct 05 '16 at 14:46
-
Ah, nice, with the knowledge of the refs/pull/ namespace my solution above becomes much easier. :-) But wouldn't this have the same problem than my solution, that the PR is not attached to your repo anymore as soon as you delete the fork? Or can you reattach somehow? – Vampire Oct 05 '16 at 15:01
-
It could be, but I hope I can get at least the code from that PR (it was one commit, therefore it could be easier) and create a new PR and new branch then. – aleskva Oct 05 '16 at 15:07
-
Yep, I just checked with GitHub staff. There is one way, but only over them. Read my updated answer. :-) – Vampire Oct 05 '16 at 15:42