在GitHub上拥有公共回购的私人分支?

我在GitHub repo中有一个公共PHP项目,它只包含一个分支(master)。

我想有一个单独的分支/分叉,对我来说是私人的(我已经为私人GitHub回购支付了费用)。我希望能够将私有分支/分叉的更改合并到公共回购,反之亦然。

考虑到这一点,以下是我的问题:

  1. 我可以在公共回购上有一个私人分行吗?
  2. 我可以将我自己的公共回购协议分叉到我自己的私人分行/分叉吗?
  3. 如果以上两种情况都有可能,哪一种是最好的出路?如果两者都不是,我应该如何进行?
125758 次浏览

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

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.

  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 public 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).

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.

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