185

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
  • 3,404
  • 5
  • 25
  • 35

4 Answers4

249

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.

Chris Watts
  • 6,197
  • 7
  • 49
  • 98
  • 29
    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. – Bruno Finger Mar 19 '19 at 10:33
  • 16
    Just want to add that forking _does_ exist on GitLab, see [here](https://docs.gitlab.com/ee/workflow/forking_workflow.html) for example. – bellackn Jul 10 '19 at 08:18
  • 7
    I had to use: pull upstream master --allow-unrelated-histories – hmojtaba Oct 15 '19 at 11:31
  • 4
    How do you submit a pull request later? – Erik Aronesty Aug 20 '20 at 18:08
  • 3
    @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 git@github.com:myuser/repo`. Then it's just a matter of `git push upstream-fork master` and submit your pull request via the GitHub site. – Chris Watts Sep 03 '20 at 10:35
  • @ChrisWatts thanks for the info about Mirroring in GitLab. I was not at all aware of it. – Paloha May 14 '21 at 12:45
  • please note "master" is changed to "main" on Github. – Zhang Fan May 10 '22 at 08:21
  • This solution worked fine for me. Thank you. – Suz'l Shrestha May 26 '22 at 10:45
  • I recommand to add `--depth=1` when pulling from a big project and you don't care about its history commits – zheyuanWang Jul 14 '22 at 04:45
  • @bellackn Do you know how to do it the other way around? If there's a repository on GitLab, can I fork it into a GitHub organization? – Nike Sep 19 '22 at 21:48
  • 1
    The page @bellackn mentioned no longer exists, I believe it lives [here](https://docs.gitlab.com/ee/user/project/repository/forking_workflow.html) now. – Melisa K. Savich Feb 09 '23 at 22:21
28

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
aksh1618
  • 2,245
  • 18
  • 37
  • 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? – Cimlman Jan 09 '19 at 10:07
  • 2
    @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. – aksh1618 Jan 09 '19 at 12:06
  • After clicking Settings -> Repository on the side bar, I can see sections Protected branches, Protected Tags, Deploy Keys, Deploy Tokens. – Cimlman Jan 09 '19 at 12:38
  • 2
    @Cimlman Are you using a custom/local gitlab instance? I have many more options in my personal, basic account on gitlab.com – aksh1618 Jan 09 '19 at 14:09
  • 2
    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. – Cimlman Jan 11 '19 at 10:30
  • This works on gitlab.com (thank you)! I now have a private repository on Gitlab.com that is mirroring a public GitHub repo. – ShreevatsaR Jun 08 '19 at 13:16
  • @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 Jan 22 '20 at 17:29
  • 2
    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. – aksh1618 May 29 '20 at 07:16
14

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.

koceka
  • 141
  • 1
  • 3
  • 2
    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). – Cimlman Oct 30 '20 at 13:27
  • Importing a project is a one-time operation. There is no way to later synchronize the imported project with the upstream to get the latest changes except for by using normal `git` commands on your local machine or in a pipeline. You could use merge requests in Gitlab (create a branch, push to it, then open a merge request and merge it) to keep things in sync as well. – Speeddymon Apr 06 '23 at 16:02
1

Forking a private github repository is possible.

  1. Click new project
  2. Select Import
  3. Select github
  4. OAuth2 used to authenticate and auto create gitlab app
  5. Select project to fork

^ I've just done this with a private repo on github, imported successfully (inc branches etc). Imported project is automatically kept private ;)

Daithí
  • 4,717
  • 5
  • 26
  • 35
  • 3
    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 Sep 06 '18 at 12:12
  • 2
    -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. – Nikos Alexandris Jan 16 '19 at 22:25
  • 1
    @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) – DGoiko Jan 22 '20 at 17:34