Forking Git repository from GitHub to GitLab

80,907

Solution 1

If you just want to track changes, first make an empty repository in GitLab (or whatever else you may be using) and clone it to your computer.

Then add the GitHub project as the "upstream" remote with:

git remote add upstream https://github.com/user/repo

Now you can fetch and pull from the upstream should there be any changes. (You can also push or merge to it if you have access rights.)

git pull upstream master

Finally, push back to your own GitLab repository:

git push origin master

If you don't want to manually pull upstream/push origin, GitLab offers a mirroring ability in Settings => Repository => Mirroring repositories.

Solution 2

The browser-only way:

  1. Create a new project in Gitlab (Just an empty project with a name is fine)
  2. Go to Settings -> Respository
  3. Enter GitHub URL under 'Mirroring repositories'
  4. Make sure 'Mirror direction' is 'Pull'
  5. Press 'Mirror repository' button
  6. Press the sync icon next to the entry that appears

Solution 3

Instead of forking, you can import any publicly available GitHub repository using only the web interface:

  1. From your GitLab dashboard click New project
  2. Switch to the Import project tab
  3. Click on the Repo by URL button
  4. Fill in the "Git repository URL" and the remaining project fields
  5. Click Create project to begin the import process
  6. Once complete, you will be redirected to your newly created project

Used this technique recently, and it works on any public repository even without a GitHub account. See this GitLab docs page for the source of info.

Share:
80,907
Cimlman
Author by

Cimlman

Updated on October 31, 2020

Comments

  • Cimlman
    Cimlman over 3 years

    Suppose that I would like to implement a fix to a project of someone else. That project resides on GitHub.

    I could create a fork on GitHub and implement the fix.

    However, I would like to create my fork on GitLab rather than on GitHub.

    Is that possible? How?

    I have read this article: https://about.gitlab.com/2016/12/01/how-to-keep-your-fork-up-to-date-with-its-origin/

    Anyway, I am not sure what should I do in my case.

    • Should I just create a fork on GitLab of the project from GitHub somehow?
    • Or should I create a mirror on GitLab of the project from GitHub?
    • Or should I create a mirror on GitLab and then fork the mirror?
    • Or should I do something completely different?

    What is the correct approach.

    Thanks.

    UPDATE

    Repository mirroring on GitLab does not make sense probably. I can create a mirror of MY GitHub repository on GitLab but I cannot create a mirror of a repository of someone else.

    https://docs.gitlab.com/ee/workflow/repository_mirroring.html

    This is what I have done so far:

    I have cloned the original GitHub project to my local machine. I have commited the fix to a new branch in my local repository. I have created an empty project on GitLab. I have set origin in my local repository to that empty project on GitLab and pushed both branches to GitLab. I have set upstream in my local repository to the GitHub repository.

    When I want to get new commits from the original GitHub repository to the repository on GitLab (i.e. sync the repositories), I can do this using my local repo as an intermediate step. However, there is no direct connection between the repo on GitHub and the repo on GitLab. Is my setup correct? Is there any difference if I make a fork on GitHub?

  • Cimlman
    Cimlman over 5 years
    Project import requires requires a "Personal Access Token". I tried to generate one on GitHub. It seems that this feature is useful when I want to create a project on GitLab from my project on GitHub. Is this assumption correct? The question is about forking a GitHub project of someone else.
  • Cimlman
    Cimlman over 5 years
    I do not have the Enterprise Edition license, i.e. I cannot see "Mirroring repositories" from step 3. Anyway, if I had such license, could I create a mirror of a GitHub repository of someone else?
  • aksh1618
    aksh1618 over 5 years
    @Cimlman That's strange, because I am also on a free license. Maybe recheck? Should be third in 'Settings->Repository'. Just to be clear: the settings on the sidebar, not the profile menu.
  • Cimlman
    Cimlman over 5 years
    After clicking Settings -> Repository on the side bar, I can see sections Protected branches, Protected Tags, Deploy Keys, Deploy Tokens.
  • aksh1618
    aksh1618 over 5 years
    @Cimlman Are you using a custom/local gitlab instance? I have many more options in my personal, basic account on gitlab.com
  • Cimlman
    Cimlman over 5 years
    We are using a self-managed installation, version 10.7.2. The latest release is 11.6. OK, this is a good point. :) I cannot try your solution on our GitLab. Anyway, the problem is obsolete for me now. It was all about our custom bug-fix of a 3rd party library. However, the bug fix has been incorporated to the official release of that library already.
  • Nikos Alexandris
    Nikos Alexandris over 5 years
    -1 because, and as the previous comment states, the question is about forking a github project of someone else. The instructions in this answer won't work in such a case.
  • Bruno Finger
    Bruno Finger about 5 years
    I would just like to complement this answer to why this is the correct approach. "Forking" is a concept created by GitHub thus only exists on GitHub, useful nonetheless. If you consider pure git itself (like a pure git server running somewhere in your network), that would be like cloning the repo to your own server and then cloning that to your local computer. You'd add the original repo as the upstream locally then. I believe that's what GitHub does behind the scenes.
  • sidgeon smythe
    sidgeon smythe almost 5 years
    This works on gitlab.com (thank you)! I now have a private repository on Gitlab.com that is mirroring a public GitHub repo.
  • bellackn
    bellackn almost 5 years
    Just want to add that forking does exist on GitLab, see here for example.
  • hmojtaba
    hmojtaba over 4 years
    I had to use: pull upstream master --allow-unrelated-histories
  • DGoiko
    DGoiko over 4 years
    @Cimlman I'm using GitLab Community Edition 11.9.1 (community =FREE version) and it DOES allow mirroring, however, free version only alows PUSH direction, not PULL, which is what the OP wanted.
  • DGoiko
    DGoiko over 4 years
    @NikosAlexandris are you sure? Can't you create an access token for your account and access someone else's repo you have read access to? (for instance, every single public repo)
  • aksh1618
    aksh1618 almost 4 years
    GitLab.com used to allow PULL, but it looks like it stopped at some point, as now it only shows the PUSH option and all my mirror configs have disappeared. Would have to resort to a cron job.
  • Erik Aronesty
    Erik Aronesty over 3 years
    How do you submit a pull request later?
  • Chris Watts
    Chris Watts over 3 years
    @ErikAronesty That's trickier as "Pull Requests" aren't a git thing, rather a GitHub thing. One way to do this though is to fork a copy on GitHub and add your forked repo as another remote, say git remote add upstream-fork [email protected]:myuser/repo. Then it's just a matter of git push upstream-fork master and submit your pull request via the GitHub site.
  • Cimlman
    Cimlman over 3 years
    OK. And if new commits are added to the original repository on GitHub, can I simply merge the changes to the repository on GitHub? This is a tricky operation and maybe it cannot be done without a intermediate repository (see last paragraph of the question).
  • Paloha
    Paloha almost 3 years
    @ChrisWatts thanks for the info about Mirroring in GitLab. I was not at all aware of it.
  • Zhang Fan
    Zhang Fan almost 2 years
    please note "master" is changed to "main" on Github.
  • Suz'l Shrestha
    Suz'l Shrestha almost 2 years
    This solution worked fine for me. Thank you.