18

I did something stupid…

  1. I fork­ed a repo on GitHub.
  2. I made some changes, commit­ted them on my fork.
  3. I sent this commit as a pull-request back to the original repo.
  4. Here comes the stupid part: I delete­d my fork.

The owner of the original repo requested a couple changes in my code before he could accept the pull-request, which I'd gladly do.

I tried re-forking the repo, but I can't checkout the commit from the pull-request, it's not even there as an "unlinked" commit (a commit that is not part of any branch or tag, I don't know the official terminology).

My question is: How can I recover the commit sent as a pull-request ?

If there's no way, re-doing the changes in a new commit is an option, but the pull-request would be lost. My question is not about not losing the changes from the commit, it's about not losing the git history, meaning keeping the commit's SHA1 (and anything else I might not be aware of).

1ace
  • 5,272
  • 4
  • 23
  • 30
  • So did you make the changes right on the website without cloning anything in git? If you did clone your fork, did you already delete the clone on your computer? – Ajedi32 Jan 07 '14 at 18:26
  • 1
    I did clone it on my computer, but I deleted it too. – 1ace Jan 07 '14 at 22:22

2 Answers2

15

It is possible to fetch pull requests to your local machine.

Without having a link to the pull request in question it's hard to test whether this will work, but you can try to

  1. create a new fork of the repository,
  2. clone your new fork,
  3. fetch your pull request from the upstream repository,

    git remote add upstream https://github.com/User/repository.git
    
    $EDITOR .git/config
    # Add `fetch = +refs/pull/*/head:refs/remotes/upstream/pr/*` to
    # the relevant section, as outlined in the linked page. Note that
    # we use `upstream` instead of `origin` as the target.
    
    git fetch upstream
    
  4. merge the pull request into your local repository, e.g.

    git checkout master
    git merge --ff-only upstream/pr/1
    
  5. and then push it back to your new fork.

If that fails, you can submit a support request to GitHub asking them to restore your repository. From an FAQ about security:

We do not retroactively remove repositories from backups when deleted by the user, as we may need to restore the repository for the user if it was removed accidentally.

To initiate this process, contact their support team as soon as possible.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • @Chris: Thanks for your answer. The following links helped me too. Both for point 3 mentioned above: http://stackoverflow.com/questions/6743514/how-can-i-fetch-an-unmerged-pull-request-for-a-branch-i-dont-own/9903203#9903203 And http://stackoverflow.com/questions/2643502/git-permission-denied-publickey/16465182#16465182 – pablofiumara Jan 12 '15 at 17:18
  • @Chris, this is pure gold! However, I am uncertain of how exactly I can add the new commits on top of the existing pull request, without having to create a new one. Pushing to the new fork does not work as expected in this case since the old pull request is coming from the unknown repository (the deleted fork). Any hints are appreciated – appoll Aug 05 '16 at 10:49
  • @appoll, I don't think you can update the old PR. You'll have to create a new one. The [documentation for Modifying an inactive pull request locally](https://help.github.com/articles/checking-out-pull-requests-locally/#modifying-an-inactive-pull-request-locally) includes steps to switch to a new branch, push that new branch to GitHub, and create a new PR. Furthermore, "The remote `refs/pull/` namespace is _read-only_", so you can't just push back to the PR ref itself. – ChrisGPT was on strike Aug 05 '16 at 10:58
  • @Chris, makes some sense. Was just wondering if I was missing something obvious. Thanks! – appoll Aug 05 '16 at 11:08
  • 2
    I tried these, git was able to fetch all the PRs too which was nice. But in the 4th step (git merge --ff-only upstream/pr/230) it returned "fatal: Not possible to fast-forward, aborting." – Saifur Rahman Mohsin Sep 15 '19 at 20:57
  • @SaifurRahmanMohsin, so the PR is probably out of date with `master`. Rebase it or do a non-fast-forward merge and deal with whatever conflicts may arise. – ChrisGPT was on strike Sep 15 '19 at 21:00
  • What about when a branch is deleted, but the fork is not deleted? I tried this and it doesn't work. I tried both the repository where the PR was opened and on the fork where the branch was. – Aaron Franke Feb 17 '22 at 15:24
2

The easiest way to do this is now with the GitHub CLI.

  1. Create a new fork, clone it:
    gh repo fork <org>/<repo>
    
  2. In your clone repo, checkout the PR:
    gh pr checkout <num>
    
  3. Push back to the fork:
    git push -u origin HEAD
    
Rich Signell
  • 14,842
  • 4
  • 49
  • 77