Git
How to Unfork a Repository on GitHub?
If you’ve forked a repository on GitHub but now want to remove the connection between your fork and the original repository, it can be a bit tricky because GitHub doesn’t provide a direct “unfork” option. However, you can still achieve this by using a workaround involving repository deletion and re-creation. This blog will walk you through the steps to safely unfork a repository on GitHub.
Why Unfork a Repository?
Some common reasons for wanting to unfork a repository include:
- Independence from the Original Repository: You may no longer need updates from the original repository or want to manage the project independently.
- Clean Project History: You might prefer to start with a clean history and avoid any reference to the parent repository.
- Clarity in Ownership: Unforking can help clarify that your project is no longer connected to the original repository’s development.
Steps to Unfork a Repository on GitHub
Since GitHub doesn’t directly support “unforking,” the best method involves creating a new, standalone repository from the forked version. This process will include the following steps:
- Rename the Existing Forked Repository: This is optional but useful if you want to keep the forked version as a backup.
- Create a New Repository on GitHub: This new repository will be the unforked version.
- Push the Forked Repository’s Content to the New Repository: You’ll copy all content, branches, and history from the fork to the new repository.
- Delete the Original Forked Repository (Optional): If you no longer need the fork, you can delete it.
Let’s go through each of these steps.
Step 1: Rename the Existing Forked Repository
If you want to keep the forked repository as a backup, consider renaming it before creating a new one. This will help avoid any naming conflicts and allow you to keep both versions.
- Go to Your Repository: Navigate to the repository on GitHub.
- Open Settings: On the repository’s main page, click on the Settings tab.
- Rename the Repository: Scroll to the Repository name section and enter a new name, then click Rename.
After renaming, your repository will remain forked, but it will have a new name, allowing you to reuse the original name in the next steps.
Step 2: Create a New Repository on GitHub
- Go to GitHub: Navigate to GitHub and log in to your account.
- Create a New Repository:
- Click on the + sign in the upper-right corner, then select New repository.
- Name your new repository (you can use the original name of the forked repository).
- Set the repository to Public or Private depending on your needs.
- Do not initialize the repository with a README or any other files (it should be empty).
- Create the Repository: Click Create repository to finalize.
Step 3: Push the Forked Repository’s Content to the New Repository
Now that you have a new, empty repository, you’ll clone the forked repository, remove its upstream reference, and push it to the new repository.
Step 3.1: Clone the Forked Repository Locally
- Open a terminal on your computer.
- Clone the repository with:
git clone https://github.com/your-username/forked-repository.git
- Change into the cloned directory:
cd forked-repository
Step 3.2: Remove the Upstream Origin
To unfork the repository, you need to remove the upstream connection to the original repository.
- Check the current remote repository settings with:
git remote -v
You’ll see output showing origin
as the remote repository connected to your fork.
- Remove the current origin reference:
git remote remove origin
Step 3.3: Add the New Repository as the Origin
Now, add your new, empty repository as the remote origin.
- Add the new repository URL as the origin:
git remote add origin https://github.com/your-username/new-repository.git
Replace https://github.com/your-username/new-repository.git
with the URL of the newly created GitHub repository.
- Push all branches and tags to the new repository:
git push -u origin --all
git push -u origin --tags
Step 4: Delete the Original Forked Repository (Optional)
If you don’t need the forked version anymore, you can delete it to avoid confusion.
- Go to the Forked Repository on GitHub: Open the repository page.
- Open Settings: Go to Settings.
- Delete the Repository: Scroll to the Danger Zone section and click Delete this repository. Confirm by typing the repository name and selecting I understand the consequences.
Confirming the Unforked Status
Once these steps are complete, you now have a new repository that is independent of the original source repository. You can confirm the following:
- No Fork Label: The new repository will no longer display the “forked from” label at the top, indicating it is independent.
- Commits and Branches: All branches and commits should be identical to the original forked repository, but without any reference to the parent repository.
- No Upstream: Running
git remote -v
in the new repository directory should only show the new repository asorigin
.
Additional Tips
- Preserving GitHub Stars and Forks: Unfortunately, stars and forks are not transferred to the new repository, as GitHub considers it separate.
- Collaborators: You’ll need to re-add any collaborators, as they won’t automatically transfer to the new repository.
- Webhooks and Integrations: If you had any integrations, webhooks, or GitHub Actions set up, you’ll need to reconfigure these in the new repository.
Conclusion
While GitHub doesn’t provide an unfork option, following this method of creating a new repository and migrating the code is an effective way to separate your project from its parent repository. This process allows you to work independently and keep your repository’s history and content intact without any connection to the original repository. By following these steps, you can easily manage your repository and maintain clarity in project ownership.