55

I have accounts in GitHub and GitLab. I generated and added an RSA key to my account in GitLab, but now I need to work with GitHub on a second project.

I know that GitLab and GitHub both use git. Please tell me if it's possible to use GitHub and GitLab on one machine?

J. Titus
  • 9,535
  • 1
  • 32
  • 45
Uladz Kha
  • 2,154
  • 4
  • 40
  • 61

3 Answers3

51

Yes you can, you can share the same key between them both (ssh key) or create a new one per git server.

Create a SSH config file

When you have multiple identity files(in your case one for gitlab and one for github) , create a SSH config file to store your various identities.

The format for the alias entries use in this example is:

Host alias 
  HostName github.com 
  IdentityFile ~/.ssh/identity

To create a config file for two identities (workid and personalid), you would do the following:

Open a terminal window.
Edit the ~/.ssh/config file. 

If you don't have a config file, create one.
Add an alias for each identity combination for example:

Host github
HostName github.com 
IdentityFile ~/.ssh/github

Host gitlab
HostName gitlab.com 
IdentityFile ~/.ssh/gitlab

This way you can have as many accounts as you wish each one with a different ssh key attached to it.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • You'd better tell people that `Host` is an alias to `HostName`, used in your command line. You can also live with just `Host`, or just `Hostname`, but then they must be an FQDN. IMHO, it's misleading to use Host alias, without the `.com`, as you give the impression that the connection string could be `git@gitlab`, or `git@github` (which works), but it should match standards, and be `git@gitlab.com` | `git@github.com`. Your current configuration above will FAIL if you issue a `git@gitxxx.com` – Fabien Haddadi May 06 '22 at 16:24
  • can you share all content of the file `config`, cause it doesn't work for me. – Dmitriy Ogureckiy Feb 18 '23 at 12:18
20

To use two different you must add your SSH key to both Git servers (Bitbucket, Gitlab, or Github) and configure git with your credentials. If both accounts use the same email address you can use:

git config --global user.name "Your Name"
git config --global user.email "email@example.com"

If the accounts use different emails, you must set up each repo with a local configuration file

git config --local user.name "Your Name"
git config --local user.email "email@example.com"

The local config will overwrite the global config. So you can use a global config for the most used account.

Antonio
  • 2,848
  • 2
  • 15
  • 15
  • You don't have to set up each repo, you can create a *.gitconfig* file in a sub-folder as described [here](https://stackoverflow.com/a/3860139/884423) – bN_ Apr 21 '21 at 18:38
14

Yes absolutely! Now that you are using ssh as the transport, you've done half the job.

GitHub and Gitlab are both remote(central) repositories. It all depends on the remote you are using to push your commits.

If you have created a project say, on GitHub, and cloned it, you will see that the remote (which is origin by default) points to the GitHub link. run $ git remote -v inside the project directory to inspect.

If you want to push the same project on GitLab, all you have to do is add another remote with a different name.

$ git remote add <different-remote-name> <gitlab-remote-link>

Now whenever you want to update a particular remote, just push to it.

Avin D'Silva
  • 330
  • 3
  • 9