简体   繁体   中英

Fork public repository into private and receive public commits

Let's have public github repository (A) https://github.com/schacon/example

I want to fork this repository to bitbucket as private repo (B).

I want to receive public commits from repository A to repository B.

Is it possible?

  • Create a new Private repository in BitBucket named B . Clone the B locally.
  • Go to repository B . Add a new remote (say, repoA) with the URL of GitHub repository (repo A).

     $ git remote add repoA <A-repo-url> $ git remote -v # see the remotes
  • Pull the commits/changes of A repository into repository B.

     $ git pull repoA master # B's master = A's origin/master
  • Push the changes to repository B (BitBucket).

     $ git push origin HEAD # B's origin/master = A's origin/master

Fetch all the branches of repository A.

$ git fetch repoA

Create a new branch with the history of A's branch (say, next )

$ git checkout -b next repoA/next    # create a local 'next' branch = repoA/next

Push the local next branch changes to Repository B's next branch.

$ git push origin next

In future, keep Sync with repository A.

$ git fetch repoA

Now pull A's branch into B's branch or, just checkout to a new branch like above example. Then Push to B's repository.

Note repoA is your repository A's URL and origin is repository B's URL here.

There's a couple of ways to do this:

  1. Just strictly mirror a repo - not trying to do development work in the local copy
  2. Do development work in your local workspace while pushing/pulling to 2 different remotes as part of your workflow.

For a mirror copy (eg all branches), its covered pretty clearly in github docs here: https://help.github.com/articles/duplicating-a-repository/ . However, note that these methods are strictly for mirroring a repo, ie they create bare local repos that aren't suitable for doing local development work.

In a nutshell:

A. For a one-time mirror copy:

$ git clone --bare https://github.com/exampleuser/A.git
$ cd A.git
$ git push --mirror https://github.com/exampleuser/B.git
$ cd ..
$ \rm -rf A.git

B. For an ongoing mirror copy:

Setup - first create target Bitbucket repo, then locally

$ git clone --mirror https://github.com/exampleuser/A.git
$ cd A.git
$ git remote set-url --push origin https://bitbucket.com/exampleuser/B.git

Then as needed, pull changes from A and push them to B

$ cd A.git
$ git fetch -p origin
$ git push --mirror

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM