3

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?

4
  • 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. Commented Oct 5, 2016 at 14:29
  • Still open, just with note: "from me/branch deleted to project/master" Commented Oct 5, 2016 at 14:30
  • *exactly "to project:master from unknown repository" Commented Oct 5, 2016 at 14:53
  • Does this answer your question? Recover a commit sent as a pull-request from a deleted fork on GitHub Commented Dec 17, 2021 at 8:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
I finally managed to do it using the 2nd way (fetch the code using your answer and creating a new PR), thank you
1

One way to fix this:

$ mkdir repo $ git init; git remote add origin [email protected]: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 = [email protected]: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

5 Comments

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.
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?
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.
Yep, I just checked with GitHub staff. There is one way, but only over them. Read my updated answer. :-)
Oh this is the best answer by far! In my case I'm looking at a historical PR, merging from a forked repo, and not only is the original feature branch deleted but the entire forked repo is gone. But of course GitHub has all of those commits "somewhere", that's how they drive the UI. And that "somewhere" turns out to be a branch called pr/9608. Perfect!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.