GitHub Create Private Fork of a Public Repository

When making a fork of a GitHub public repository, the fork repository is set up with public visibility. However, under certain circumstances, it may be desirable to make the fork repository with private or internal (in an organization) visibility.

Here is one way to indirectly 'forking' a repository with with private or internal visibility.

First creates a new repo (private-repo.git) in Github through the ui or cli.

Then, clone a public repo to local.
--bare option is use to make a copy all branches and tags without mapping them to the remotes origin

Then, push the local to the private repo
--mirror option is use to push all refs

Remove the local copy of public repo .git directory.
git clone --bare

cd public-repo.git
git push --mirror

cd ..
rm -rf public-repo.git
To perform any changes,

First clone the private copy to local.
Make changes and push the changes up to private repo.
git clone
cd private-repo

git commit
git push origin master
For any subsequent changes on the private repo, pull it down to local
git pull origin # or git pull origin branch

# or using fetch/merge

git fetch origin
git merge origin/master # or intended remote branch
To update from public repo to private repo.
First add the public repo to remote. Now there should be two remotes.
One for origin pointing to the private repo (your_user), and one as public pointing to public repo (original_user)

Then pull the latest codes from public to local
Then push to the private repo

To also update all the tags from the public repo to private repo
fetch the public repo tags
push the tags to private repo
cd private-repo
git remote add public

# check list of remote
git remove -v

git pull public master # Creates a merge commit
git push origin master

git fetch public --tags
git push origin --tags

No comments:

Post a Comment