222

I have a public PHP project in a GitHub repo, which contains just one branch (master).

I want to have a separate branch/fork that is private for me (I have paid for private GitHub repos). I would like to be able to merge changes from the private branch/fork to the public repo, and vice versa.

With that in mind, here are my questions:

  1. Can I have a private branch on a public repo?
  2. Can I fork my own public repo into my own private branch/fork?
  3. If both of the above are possible, which is the best way forward? If neither, how should I proceed?
strupo
  • 188
  • 1
  • 3
  • 14
Lizard
  • 43,732
  • 39
  • 106
  • 167
  • 2
    Isn't the best idea to fork your public repository as a private repository, where you create several branches that you don't push (back) to the public repository (and vice versa)? – Legolas Nov 02 '11 at 15:50
  • 2
    Thats what I am asking, I want to be able to create a copy of public repo but my updates to be private (unless i want to push specific things back to public one) – Lizard Nov 02 '11 at 15:56
  • 1
    http://24ways.org/2013/keeping-parts-of-your-codebase-private-on-github/ – assylias Jun 06 '15 at 05:39
  • @assylias, could you put it as an answer? it's the best answer in my opinion, and I didn't pay attention to your comment before finding the article myself. – mbh86 Jul 18 '19 at 15:33
  • @mbh86 It's not really an answer, being a link, and I don't have time to turn it into an answer. But if you want to use the content of that page to create an answer here (with a reference to the original site), it could certainly help other users and you would get my upvote :-) – assylias Jul 18 '19 at 19:31
  • @Lizard any chance you could change the accepted answer to @mj1531's? It looks like a lot of people (including me!) are getting confused by an answer that's not actually possible. – Tom Mar 19 '20 at 16:24

5 Answers5

169
  1. Duplicate your repo.
  2. Make the duplicated repo a private one on GitHub.
  3. Clone the private repo to your machine
  4. Add a remote to your private repo (git remote add public git@github.com:...)
  5. Push branches with commits intended for your public repo to that new public remote. (make sure you don't accidentally commit private-only code)
  6. You can bring in changes to your public repo using 'git fetch public' and then merge them locally and push to your private repo (origin remote).
pridegsu
  • 1,108
  • 1
  • 12
  • 27
mj1531
  • 2,456
  • 1
  • 19
  • 10
  • 3
    Duplication works fine for me, though I had to do it in the opposite order, first creating a private repo and then duplicating the public repo there from the command line. Thanks! – Joel Jan 26 '13 at 19:48
130

Is it possible to have a private branch on a public repo?

On GitHub, your repository is either public or private; you cannot selectively "privatize" just a branch.

Can I fork my own public repo into my own private branch/fork?

You can clone your public repo to your local machine, branch as needed, and simply not push your "private" branches upstream (by specifying which branch to push to origin: git push origin master or git push origin branch-i-want-to-be-public:master).

Which is the best way forward/how should I proceed?

In order to take advantage of GitHub for both your public and private development, I would suggest forking your public branch within GitHub, changing the settings of the new fork to "Private", and then cloning the private version down to your local machine. When you're ready to make changes public, push everything up to your private fork on GitHub and then use pull requests to selectively copy branches to the public repo.

To make a repository private on GitHub, you must have an upgraded (paid) account. If you're only rocking the free account, you can still use the first process I suggested — clone public to local machine, branch, and push specific "public" branches to origin — without needing a private repo.

If you have a paid GitHub account, or are using another service that offers public and private forks and pull requests (such as BitBucket), then you can use either of the above approaches to make your code public.

Justin ᚅᚔᚈᚄᚒᚔ
  • 15,081
  • 7
  • 52
  • 64
  • 45
    GitHub does not allow you to make a fork of a public repo private. You'd have to duplicate the repo to make it private, and then you'd lose the connection to the public repo. I wonder if you can add the public repo as a separate remote to your local clone of the private repo. Then push branches to that remote. – mj1531 Jan 09 '13 at 17:32
  • 3
    The implication of this unfortunately being that it's not possible to use pull requests to pull changes from a private "fork" to a public copy of the repository. – Michael Mior Jun 24 '16 at 14:23
  • 12
    GitHub now offers unlimited private repositories to free accounts. – Nathan F. Jan 10 '19 at 16:48
  • 6
    This really shouldn't be the top answer given that the recommend workflow (PR from a private fork) doesn't work. The answer by @mj1531 is a better workflow. – stewSquared Apr 12 '19 at 22:37
26

There is another solution which I find better as it doesn't result in duplicate repos on the same machine.

  • Make a branch with the stuff you want private.
  • Make a new repo on GitHub, set it to private.
  • Add new GitHub repo as a second remote to your repo on your machine.
  • Push private branch to second remote.

End result is 1 repository with 2 remotes. 1 public, 1 private.
Just need to be careful about which you push to so name accordingly.

Arthur Bowers
  • 581
  • 5
  • 9
-1

To create private branch (downstream) of a public repository (upstream):

Initialize repository

$ git init private-repo
$ cd private-repo

Add remotes

$ git remote add upstream git@github.com:<username>/public-repo.git
$ git remote add origin git@github.com:<username>/private-repo.git
$ git remote --verbose

Initial commit

$ git commit --allow-empty --message "Initial commit"
$ git push --set-upstream origin master

Create development branch

$ git checkout -b develop
$ git push --set-upstream origin develop
$ git branch --all

Merge upstream into development branch

$ git fetch upstream master
$ git merge --allow-unrelated-histories upstream/master
$ git push

Some changes on repository...

# Do some changes...
$ git add .
$ git commit -m "Some changes"
$ git push

Apply upstream changes...

$ git fetch upstream master
$ git log --all --graph --oneline
$ git merge upstream/master
$ git push

Merge development branch to master

$ git switch master
$ git merge develop
$ git push
$ git log --all --graph --oneline

For next clones:

Clone repository

$ git clone git@github.com:<username>/private-repo.git
$ cd private-repo

Add upstream remote

$ git remote add upstream git@github.com:<username>/public-repo.git
$ git remote --verbose

Switch to development branch and get upstream changes

$ git switch develop
$ git fetch upstream master
$ git log --all --graph --oneline
$ git merge upstream/master
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
-1

1.) Is it possible to have a private branch on a public repo

From what I know, no.

2.) Can I fork my own public repo into my own private branch

No, you can't fork a full repo (1-n branches) into a single branch. Well actually you could, if you just fork the one branch of the full repo. Just add it as a remote or start from a clone.

You might also be interested in Sparse checkouts.

3.) If both the above the are possible which is the best way forward

n/a

4.) If neither are possible how should I proceed?

n/a

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836