212

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/user/project/repository/mirror/

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?

5 Answers 5

289

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.

10
  • 34
    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. Commented Mar 19, 2019 at 10:33
  • 18
    Just want to add that forking does exist on GitLab, see here for example.
    – bellackn
    Commented Jul 10, 2019 at 8:18
  • 8
    I had to use: pull upstream master --allow-unrelated-histories
    – hmojtaba
    Commented Oct 15, 2019 at 11:31
  • 4
    How do you submit a pull request later? Commented Aug 20, 2020 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 [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. Commented Sep 3, 2020 at 10:35
29

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
8
  • 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
    Commented Jan 9, 2019 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
    Commented Jan 9, 2019 at 12:06
  • 2
    @Cimlman Are you using a custom/local gitlab instance? I have many more options in my personal, basic account on gitlab.com
    – aksh1618
    Commented Jan 9, 2019 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
    Commented Jan 11, 2019 at 10:30
  • 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
    Commented May 29, 2020 at 7:16
15

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.

2
  • 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
    Commented Oct 30, 2020 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
    Commented Apr 6, 2023 at 16:02
2

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 ;)

4
  • 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
    Commented Sep 6, 2018 at 12:12
  • 3
    -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. Commented Jan 16, 2019 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
    Commented Jan 22, 2020 at 17:34
  • Tried and seems to be a copy/import what's good is that it includes all the commit. But this is still not a fork of the original project. Ideally if the original repo change, the imported gitlab doesn't.
    – Han
    Commented Apr 11 at 4:55
0

This has solved it for me. Not a direct fork, but all the branches and commits are pushed.

git push --set-upstream git@<<yourgitlabpath>>$(git rev-parse --show-toplevel | xargs basename).git $(git rev-parse --abbrev-ref HEAD)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.