Recover a commit sent as a pull-request from a deleted fork on GitHub
Categories:
Recovering Commits from a Deleted GitHub Fork

Learn how to retrieve your valuable commits even after the GitHub fork they originated from has been deleted, ensuring your work is never lost.
It's a common scenario: you fork a repository, make some changes, create a pull request, and then, for various reasons, delete your fork. Later, you realize you need those specific commits again. Perhaps the original pull request was closed without merging, or you want to re-apply those changes to a different branch or repository. This guide will walk you through the process of recovering those 'lost' commits, leveraging Git's powerful history tracking and GitHub's event logs.
Understanding the Challenge
When a fork is deleted on GitHub, the repository itself is removed, and with it, direct access to its branches and commit history. However, Git is a distributed version control system. If your pull request was ever opened, GitHub retains a record of the commits associated with that pull request, even if the source fork is gone. The key is to find the commit SHA (Secure Hash Algorithm) of your changes. Once you have the SHA, you can fetch that specific commit into any local repository.
flowchart TD A[User Forks Repository] --> B{Makes Changes & Commits} B --> C[Creates Pull Request] C --> D{Fork Deleted?} D -- Yes --> E[Commit SHAs still exist in PR history] D -- No --> F[Commits accessible via fork] E --> G[Retrieve Commit SHA from PR] G --> H[Fetch Commit into Local Repo] H --> I[Reapply Changes (e.g., cherry-pick)] I --> J[Success!]
Workflow for recovering commits from a deleted fork
Step-by-Step Recovery Process
The recovery process primarily involves identifying the commit SHA from the original pull request and then using Git commands to bring that commit into your current working repository. This method assumes you still have access to the original upstream repository where the pull request was made.
1. Locate the Original Pull Request
Navigate to the original (upstream) repository on GitHub. Go to the 'Pull requests' tab. Even if your fork is deleted, the pull request you submitted will likely still be listed, possibly marked as 'Closed' or 'Merged' (if it was). Click on the pull request to view its details.
2. Identify the Commit SHA
Within the pull request details page, look for the 'Commits' tab. Here, you will see a list of all commits that were part of that pull request. Each commit will have a unique SHA-1 hash (a long string of hexadecimal characters). Copy the SHA of the commit you wish to recover. If there were multiple commits, you might want the SHA of the merge commit or the last commit in your branch.
3. Clone or Navigate to a Local Repository
If you don't already have a local clone of the original upstream repository, clone it. If you do, navigate to its directory in your terminal. This is crucial because you'll be fetching the commit into a local copy of the repository that could have received your original PR.
4. Fetch the Commit by SHA
Use the git fetch
command with the commit SHA you copied. This command tells Git to retrieve the specific commit object and its history from the remote, even if it's not directly reachable by a branch name. You'll typically fetch from the origin
remote, which points to the upstream repository.
5. Cherry-Pick or Rebase the Commit
Once the commit is fetched, it's available in your local repository's object database. You can now apply it to your current branch. The safest and most common way is to cherry-pick
it. If you need to apply multiple commits in sequence, you might consider an interactive rebase, but cherry-pick
is usually sufficient for a single commit or a small, isolated set.
6. Push Your Changes (Optional)
After successfully applying the commit(s) to your local branch, you can push this branch to a new fork or a new branch in the original repository (if you have permissions) to re-submit your work or integrate it elsewhere.
# 1. Clone the original upstream repository (if you don't have it)
git clone https://github.com/original-owner/original-repo.git
cd original-repo
# 2. Ensure your local repository is up-to-date
git fetch origin
git checkout main # Or the branch you want to apply changes to
git pull origin main
# 3. Fetch the specific commit using its SHA (replace <COMMIT_SHA>)
git fetch origin <COMMIT_SHA>
# 4. Cherry-pick the commit onto your current branch
git cherry-pick <COMMIT_SHA>
# If you encounter conflicts, resolve them and then:
git add .
git cherry-pick --continue
# 5. (Optional) Push the changes to a new branch or fork
git push origin HEAD:new-feature-branch
Git commands to fetch and cherry-pick a specific commit
git pull
) before attempting to cherry-pick or rebase, to minimize potential merge conflicts.Alternative: GitHub's 'Compare' View
Sometimes, finding the exact commit SHA can be tricky if the pull request history is very long or complex. GitHub provides a convenient way to view the diff of a pull request, which implicitly references the commits. You can often find the commit SHA directly in the URL or the UI when viewing the 'Commits' tab of a pull request. If the PR was merged, the merge commit SHA is also a good starting point.