-
Create a new private repository on Github
-
Fork the repo (
BASE_REPO_URL) to your new private repo (PRIVATE_REPO_URL) as follows:BASE_REPO_URL=<BASE> # remote URL of repo you are forking PRIVATE_REPO_URL=<PRIVATE> # remote URL of your new private fork repo # Create a bare clone of the base repo git clone --bare $BASE_REPO_URL # Mirror-push your bare clone to your new private repo cd ${BASE_REPO_URL##*/} git push --mirror $PRIVATE_REPO_URL # Remove the temporary local repository you created in step 2 cd .. rm -rf ${BASE_REPO_URL##*/}
cd to your preferred workspace, then clone your private fork
git clone $PRIVATE_REPO_URL # clone the private fork
cd ${${PRIVATE_REPO_URL##*/}%%.git} # cd into the cloned directory
# add the original repo as remote to fetch (potential) future changes.
git remote add upstream $BASE_REPO_URL
# disable push on the remote (as you are not allowed to push to it anyway).
git remote set-url --push upstream DISABLE-
You can list all your remotes with
git remote -v. You should see:origin <PRIVATE_REPO_URL> (fetch) origin <PRIVATE_REPO_URL> (push) upstream <BASE_REPO_URL> (fetch) upstream DISABLE (push) -
When you push, do so on
originwithgit push origin. -
When you want to pull changes from
upstreamyou can just fetch the remote and rebase on top of your work.git fetch upstream git rebase upstream/main
Additional information on creating a private fork by duplicating a repo is documented here.
see also: https://ellisbrown.github.io/programming/git-private-forks/#basic-steps

@lestephane thanks for the comments! Glad to see others have use for this.
Good suggestion---it was not clear. I updated the variable names and comments to hopefully make it clear that both should be remote URLs (SSH or HTTPS should work).
Yes, a directory ending in
.gitis expected when cloning with the--bareoption. This should not be an issue though; you should be able tocdinto it usingcd ${BASE_REPO_URL##*/}.The
##*/is some bash variable mangling to extract everything after the last/in the URL.Consider the following example:
I've used this on a various computers with git >2.0. Just verified with
2.32.1.