-
Notifications
You must be signed in to change notification settings - Fork 2
Copying a private branch to a public repository
If you want to make a private branch in EpiModel/EpiModelHIV-p public---for instance, because you've published a paper relying on the code---the following steps should allow you to do so while retaining the full commit history linked to your specific branch.
-
Create a new public repository in GitHub. For the purposes of the example, assume the new public repository is at
EpiModel/my-public-repo
. -
Clone your private branch, we'll call it
my-private-branch
, into a new directory on your local machine. Then, move into that directory:
git clone --branch my-private-branch --single-branch [email protected]:EpiModel/EpiModelHIV-p.git new-local-dir && cd new-local-dir
The --single-branch
flag tells git to download the code on your branch and the linked commit history from the tip of your branch back to the beginning of the repo.
- At this point, the cloned repo will still list the original remote URL. This line updates the remote URL named "origin" to the remote URL of the new empty repo I created to store my analysis code publicly.
git remote set-url origin [email protected]:EpiModel/my-public-repo.git
- Create a new branch called
main
.
git branch -M main
- Re-checkout
my-private-branch
.
git checkout my-private-branch
- Push
my-private-branch
to themain
branch on the remote repository. After running this line, the code and git history should appear in the main branch of the your public repo on Github.
git push -u origin my-private-branch:main
- Update the git configuration for your public repo as follows:
git config remote.origin.fetch +refs/heads/main:refs/remotes/origin/main
git config branch.main.merge refs/heads/main
After completing these steps, you should now be able to continue developing on the new public repo just as you would any other (if you want to).
This example was cobbled together based on tidbits from some of the following resources, along with a fair bit of trial and error: