70

There exist a public repo for Quick framework here. I'd like to be able to fork this into a private enterprise GitHub repository. Forking would allow all the branches to remain.

the alternative would be to clone the repo and push up only a single branch to the enterprise but then I lose on not having all the branches from the source/original.

update: I ended up pushing all my branches into the enterprise git. if you just do a git push yourRemoteName myNewBranch then it will push the code into that branch on the enterprise git while creating that branch in enterprise GitHub.

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
j2emanue
  • 60,549
  • 65
  • 286
  • 456

3 Answers3

47

It's not possible, because your GitHub Enterprise (GHE) installation is separate from the public one, so there's no way for the two systems to track each other's branch relationships.

The best you can do is exactly as you describe: Clone the repo, then push it to your GHE installation, and yes, you will then lose the branching relationships across repos.

The other option would be to keep a fork on the public GitHub (GH) repo--possibly keeping it in sync (manually, or with a cronjob) with your GHE repo. Then you'll have two instances of your repo, and the public one would retain branch relationships with the original repo.

Depending on why you need to put this on GHE, it may or may not work. If you're making private contributions, it clearly won't work--as your private contributions would no longer be private. If you want it on GHE due to some corporate policy that all open source projects used internally are kept on the GHE, or something similar, then it would work, with the added administrative overhead of keeping the repo in sync two places.

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
38

While it isn't possible to fork from the public GitHub directly to your Enterprise installation, you can fork it on the public GitHub and then mirror that forked repository on your enterprise installation.

The examples show how to do it without GitHub CLI.

Steps

  1. Create an empty repository on your enterprise GitHub:
    curl https://github.yourenterprise.com/api/v3/user/repos \
      -H "Authorization: Bearer YOUR-TOKEN" \
      -u "yourUsername" \
      -d "{\"name\": \"whatever-repository\", \"private\": true }"
    
  2. Create a bare clone of your fork
    git clone --bare https://github.com/publicGitHubUser/forked-repository.git
    
  3. Change directories so you are inside the bare clones folder:
    cd ./whatever-repository.git/
    
  4. Push the repository with the --mirror flag to your enterprise GitHub
    git push --mirror https://github.yourenterprise.com/enterpriseGitHubUser/forked-repository.git
    

More Information

Create a Token: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token

Duplicate a Repo: https://help.github.com/articles/duplicating-a-repository/

Rest API Intro: https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api

Edit: Credit to @yuri-feldman for update when authenticating with your personnel access token via curl.

Pytry
  • 6,044
  • 2
  • 37
  • 56
  • 1
    Would it be possible to add a screenshot to show how this looks at the end. – tasomaniac Sep 19 '19 at 09:46
  • 1
    This answer is really helpful but, as a git "paduan," I have no idea what to do next. I want to fork the project github.com/foo.git and work on it on enterprise.github.com/bar.git. So I followed all your steps. I have foo.git on my computer, and bar.git on enterprise (through a simple name change of the repo). bar.git is a clone of foo.git, as expected. But foo.git on my computer is a bare repo and has nothing I can edit. I gather I need to clone bar.git onto my computer so I can edit, and then push that to enterprise? But how do I update bar.git whenever foo.git changes? – AstroBen Jun 12 '20 at 18:28
  • @AstroBen can you ask this as a question on stackoverflow or github ? I am also interested to know the answer – DevX Apr 30 '21 at 06:02
  • 1
    It is now possible to create the repository and push it with one [GitHub CLI](https://cli.github.com/) command: ```gh repo create github.your-enterprise.com/your-organization/repo-mirror --source=./forked-repo --remote=upstream --push``` – Kamil Górzyński May 12 '22 at 13:38
  • As of now need to provide a token to use GitHub web api, i.e. in the curl line add `--header "Authorization: Bearer YOUR-TOKEN"`. See more here https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api – Yuri Feldman Nov 09 '22 at 12:38
1

If you use GitHub Enterprise then you automatically get access to GitHub Enterprise Cloud i.e. https://github.com/enterprises/mycompany, with the same licence count. This can be linked with SAML to your in-house Active Directory. Then you can simply fork it in GitHub yet keep it within your company's control.

Sadly you will still need to mirror clone it if you really need it on your in-house instance. However as you control both ends now, you should be able to use GitHub Actions (or Apps) to more intelligently keep them in sync.

Martin
  • 2,316
  • 1
  • 28
  • 33
  • If your company is exclusively using their own self-hosted github enterprise server, then storing any corporate-owned commits to the repo on the third-party, github enterprise cloud would likely get you in trouble. – Carl Walsh Jan 14 '21 at 22:11
  • True, but the OP is talking about forking a public/open source repo. In which case it is highly unlikely that any code you wrote for that could be kept private (at least if you intended to use in a product) as that would break the terms of the OS licence, – Martin Jan 15 '21 at 10:33
  • 1
    At big-tech companies I have worked at, it's not uncommon to use that a private fork with patches/hacks unsuitable for general use, and not do the work of contributing back to upstream. Also, a lot of software is written for internal use, either as test or infrastructure code. – Carl Walsh Jan 15 '21 at 16:31