Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save milesrichardson/b00e2623e5f4427ec192b1f632c3c7dc to your computer and use it in GitHub Desktop.
Save milesrichardson/b00e2623e5f4427ec192b1f632c3c7dc to your computer and use it in GitHub Desktop.
How to keep GitHub forks up to date by leveraging GitLab repository mirroring to and from GitHub

What you want

  • A public fork of a public GitHub repository, say facebook/react
  • A private fork of the same public GitHub repository
  • Keep both forks always up to date with all branches of the upstream facebook/react repository
  • Selectively publish branches from the private fork to the public fork in order to submit pull requests to facebook/react

What you need

  • A GitHub account
  • A GitLab account
  • A GitHub fork of the upstream repository
  • A GitHub personal access token with public_repo access checked

What to do

  1. In GitLab, "create a project"
  2. After project created, in GitLab, go to Settings -> Repository, expand "Mirroring"
    • Setup "push" mirror to your public GitHub fork
    • Use HTTPS clone URL with personal access token as password
    • Only mirror protected branches
  3. Setup protected branches in GitLab, Settings -> Repository -> Protected branches
    • Set the default branch of the upstream as protected
    • Choose a prefix, something like pr/*, and mark it as protected

How to work

  1. Clone your private GitLab repository. Treat the default branch as the default branch of the upstream.
  2. Create a branch off the default branch and do some private work on it (not ready to publish yet)
  3. Make sure the branch does not begin with your prefix, so does NOT begin with e.g. pr/* when pushing it to GitLab
  4. When ready to publish, push the branch to GitLab to a new branch with the pr/* prefix
  5. The pr/* branch will now exist at GitHub (you can manually sync if you don't want to wait 5 minutes)
  6. Submit a pull request to upstream using regular GitHub workflow from the pr/* branch
@milesrichardson
Copy link
Author

Note: this no longer works without GitLab premium :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment