aleskva
aleskva

Reputation: 1805

How to recreate a branch from pull request?

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?

Upvotes: 3

Views: 2054

Answers (2)

Vampire
Vampire

Reputation: 38619

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.

Upvotes: 2

ddavison
ddavison

Reputation: 29032

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

Upvotes: 1

Related Questions