GitHub doesn't allow making a private fork of a public repository. Their rule is: once public, always public. The usual way to get around this is to clone the repository to your computer and then push it to a new private repository. But there's an easier way to do this directly in GitHub's interface.
Creating a Private Copy Using Import Repository
You can create a private copy without touching the terminal:
- Go to the public GitHub project and copy its URL
- Click the plus button in the top-right corner of GitHub and select 'Import repository'
- Paste the URL, give your new repository a name, and set it to Private
- Click 'Begin Import' and GitHub will copy everything, including all commit history
When the import finishes, you'll have an exact copy of the original project with all its code and history, but it's private instead of public.
Keeping Your Private Copy Updated
Unlike a traditional GitHub fork, you can't use the sync button you might be familiar with in the GitHub UI. But don't worry - you can still keep your private repo in sync with the original. You just need to add the source repo as a remote:
- Copy the URL of the original repository
- In your local repository folder, run
git remote add upstream <original-repo-url>
- Get the latest code by running
git fetch upstream
- Pull in the changes with
git merge upstream/main
This lets you grab changes from the original project while keeping your work private. Just remember that you'll need to run git fetch upstream
first whenever you want to get the latest updates.
Cherry-Picking Specific Commits
Sometimes you don't want all the changes from the original repo - just certain ones. You can cherry-pick exactly what you need:
- Find the commit ID you want from the original repository
- Run
git fetch upstream
to get the latest code - Grab just that one commit with
git cherry-pick <commit-id>
- Push it to your repo with
git push
This way you can grab just the bits you want without taking everything.
Starting Fresh with a Clean History
When starting a new project from a template, you might want to clean up the commit history. Check out my bash script that I include in my SAAS open source template.
Before running the script, you might want to change the initial commit message. Just edit line 24:
git commit -m "Starting from fresh with Steady Start template"
After running this script, your repository will have just one commit. Remember that once you do this, you can't use git merge
or git cherry-pick
with the original repository anymore.
Conclusion
These methods give you several ways to work with public code while keeping your changes private. Whether you need a quick private copy, want to stay in sync with the original project, or start fresh with a clean history, these tricks make it much easier than the traditional approach.