How to fork a GitLab repository to GitHub?

Viewed 4587

I would like to fork a GitLab repository (not my own, and public), so I can make changes to my fork, and update that fork with the upstream version whenever there are upstream updates, using GitHub desktop.

So far, what I have done is:

(1) imported the GitLab repo on github.com -> import repository , the repository appears on GitHub as my own (which it is not) and not as a fork (which I want it to be)

(2) on GitHub desktop, add repository -> clone GitHub repository from (1) on my local machine, make changes to it, push the changes to my GitHub repository (good)

(3) if there are any changes made to the upstream GitLab repository, I cannot merge those into my GitHub repository. Indeed, the latter is not considered a fork and it is not connected in any way to the upstream GitLab repository. Normally I would use on GitHub desktop "Choose a branch to merge into master", choose the upstream branch, and merge the changes.

(4) I tried "git remote add upstream {gitlab repo}" but that did not change anything.

(5) also tried, alternatively, just cloning the GitLab repository directly on my local machine, making changes to it, but pushing my changes attempts to push them on GitLab, which I cannot do and do not want to do as the repo isn't mine.

2 Answers

What you are asking for is unfortunately not possible using the Gitlab and GitHub websites. What you can do is to add the upstream remote locally and merge the changes manually.

What you did in step 4 is already a step in the right direction. What you need to do in order to merge the upstream is to fetch and merge. Like this:

git remote add upstream {gitlab repository}

Now fetch the upstream. This will download commits, files, and refs from the Gitlab repository.

git fetch upstream

Then you can merge branches from the upstream into your local repository. Be aware that you may have to resolve conflicts.

git merge upstream/master

To publish the changes, push them to the origin. In this case GitHub. The origin remote is the default namespace, meaning that when you pull the master branch, for example using git pull master, the GitHub master branch is pulled.

git push

Sources:

You are almost there with updating your GitHub repo with the remote changes from GitLab. All you need to do is:

git fetch <gitlab remote> # or git pull or pull --rebase

Then you push to your GitHub repo

git push <github remote> <branch name> 

Basically, your workflow is pull from GitLab -> push to GitHub. A more detailed answer could be found here.

I am not aware for any existing integration between GitLab and GitHub, that would classify your repository as forked on GitHub.

You can do this process as often as you would like. It's just manual work for you.

If you are looking into automating this process, you can take a look at cron jobs, but that is a totally different topic.


(4) I tried "git remote add upstream {gitlab repo}" but that did not change anything.

This just adds another remote to your list of remotes. By itself, it doesn't do anything. You have to push to that remote in order to start making changes.


Also, since you are doing this, check if the project you are working on if it has a LICENSE. That would be helpful in such a case.

Related