Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have one Git repo placed on Github and Gitlab?

Tags:

git

github

gitlab

Is it possible to have one git repo placed on both github and gitlab? So that if you push to a remote repo that its both on Github and Gitlab.

like image 596
Luuk Kenselaar Avatar asked Jun 04 '18 18:06

Luuk Kenselaar


1 Answers

You have misunderstood how Git works. Or how content hosting works.

First of all, your repo is not on GitHub nor GitLab. Your repo is on your hard disk, right there where you cloned or created it. This is a repository. Local one. This is where you do the work *)

On GitHub and on GitLab you can setup remote copy of your repository. Then, after you did some work on your local repo, you can instruct your local repo to send those new bits of work to that remote copy. Or to another remote copy. Or to 50 remote copies held in different places.

Second fact is, GitHub and GitLab are separate. In fact, there is one GitHub, but there is no single GitLab. There are many GitLabs out there, you can even buy a server and host your own GitLab. This only emphasises that they are all separate. Whatever you send to GitHub will NOT magically show up on GitLab1, nor GitLab2, nor (..)

Third fact, they are all separate, but they all allow you to host copies of your git repo. Since you can instruct your local repo to send things to multiple remotes, you can have them all in sync.

However, pasting all the URLs to all servers each time you want to do a push would be cumbersome, so your local git repo can be taught to know the remote locations and remember them for you. This called remotes and you probably saw it already in form:

git push origin master
          \        \
           \        branch to be pushed
            name of the remote

origin is the typical name for the-single-remote-server you work with. But if you want to have many, you probably want to set some meaningful names, like:

git remote add remote-github (url-to-repo-on-github)
git remote add remote-gitlab1 (url-to-repo-on-gitlab1)
git remote add remote-gitlab2 (url-to-repo-on-gitlab2)
...

This sets up 3+ remotes with different names. Now you can:

git push remote-github master
git push remote-gitlab1 master

and so on. That will work - basically, do code changes, commit, push them to 3+ servers, and you have "same things on GitHub and GitLab". Keep in mind that if you are not alone and if you let someone work on your code on GitHub/GitLab separately, everyone would need to push to all remotes at the same time and in the same order, or else it can diverge quickly.

Manually pushing to 2+ remotes can be somewhat cumbersome too, even if they are remembered by their names. That's why recent Git versions allow to set multiple push URLs under single name of a remote.

Also, Git repositories, and some Git hostings, support "triggers" or "hooks". These things allow you to set a script that will be executed every time something happens. Something like a commit or push. Using that you could instruct your hosting to, for example, at every received push do a push to the other remote repo. But I won't cover it, here&now, I think you already have enough keywords to search for!

*) well, except for some semi-automated things like PRs/MRs/Reviews/Issues/etc - but that's specific to your workflow and support for it on the hosting site. Obviously, since sites are separate, and since support for features may differ, PRs/MRs/Reviews/../etc performed on one site will not magically transfer to other sites. If the specific feature is implemented via branches, then you can sync that via some hooks&scripting. Otherwise, you'd need to find a proper plugin for the site. Or, well, write it yourself. IIRC, at least GitLab has an API for that.

like image 91
quetzalcoatl Avatar answered Oct 11 '22 15:10

quetzalcoatl