Skip to content

Instantly share code, notes, and snippets.

@twni2016
Last active October 8, 2025 13:50
Show Gist options
  • Save twni2016/3533cce4b36599d8956b57bf6a359722 to your computer and use it in GitHub Desktop.
Save twni2016/3533cce4b36599d8956b57bf6a359722 to your computer and use it in GitHub Desktop.
How to fork a public repo into a private repo (support pull request)

Disclaimer

Following the fantastic answer

What we want?

There is a public repo we want to fork, but we want to make our forked repo private, and also can make a pull request to the public repo.

Setup

Basic idea: we create a public forked repo as usual, but serving as an agent.

The relationship in remote add (A <- B means cd A; git remote add B):

Forked Repo <- Private Repo <- Public Repo

But in the github UI of the Forked Repo, we can still make a pull request to the Public Repo. So the actual dependency graph is cyclic: Forked Repo -> Public Repo where -> means making pull request.

Mirror Public Repo into Private Repo

First create an emtpy private repo https://github.com/yourname/private-repo.git, then run

git clone --bare https://github.com/exampleuser/public-repo.git
cd public-repo
git push --mirror https://github.com/yourname/private-repo.git # mirror the public repo into private repo 
cd ..
rm -rf public-repo # we don't use it anymore

Clone Private Repo

git clone https://github.com/yourname/private-repo.git 
cd private-repo
git remote add public https://github.com/exampleuser/public-repo.git # add remote of public

Clone a real Forked Repo

Use the GitHub UI to create a fork of the public repo (the small "Fork" button at the top right of the public repo page). Then:

git clone https://github.com/yourname/the-fork.git
cd the-fork
git remote add private_repo_yourname https://github.com/yourname/private-repo.git # add remote of private

Usage

Pull from Public Repo

git pull in our private repo directly

cd private-repo
git pull public <branch> # Creates a merge commit
git push origin <branch>

Make Changes in Private Repo

Just feel free to make changes in the private repo in any branches we like.

Create a branch based on a local branch

git checkout -b <new branch> <old branch>
# git checkout -b <new branch> # is the default old branch = main

When making pull request, we can change the base branch as the parent branch (in the github website) that current branch inherits, for better visualization in comparison.

Pull request to Public Repo

We have to use the forked repo as an agent:

cd the-fork
# if the branch does not exist:
git checkout -b <branch> 
# else
git checkout <branch> 

git pull private_repo_yourname <branch>
git push origin <branch>

Then use github UI to make a pull request!

@Kalyansayy
Copy link

commit the changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment