Skip to content

Copying a private branch to a public repository

Jason Gantenberg edited this page Jun 25, 2022 · 5 revisions

Making Code Public

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.

  1. Create a new public repository in GitHub. For the purposes of the example, assume the new public repository is at EpiModel/my-public-repo.

  2. 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.

  1. 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
  1. Create a new branch called main.
git branch -M main
  1. Re-checkout my-private-branch.
git checkout my-private-branch
  1. Push my-private-branch to the main 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
  1. 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).

Other Resources

This example was cobbled together based on tidbits from some of the following resources, along with a fair bit of trial and error: