3

In GitLab, I own a group, and within this group I have a private repository. It's url looks like this: https://gitlab.com/groupname/reponame.

On my machine, I have an ssh key pair id_rsa_gitlab & id_rsa_gitlab.pub. I have added the public key in GitLab settings. And I have added the following code in my ~/.ssh/config:

# GitLab.com
Host gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_gitlab

I have tested that the communication works using ssh -T git@gitlab.com as in the docs.

Now, I would like to clone my group's private repo. But unfortunately, none of the following works without manually typing my GitLab username and password (not the password of the SSH key, but the password for my GitLab account).

# Asks for username and pass and then clones successfully
git clone https://gitlab.com/group/repo  
git clone https://gitlab.com/group/repo.git
git clone https://git@gitlab.com/group/repo
git clone https://<myGitLabUser>@gitlab.com/group/repo

# Asks only for password and then clones successfully
git clone https://<myGitLabUser>@gitlab.com/group/repo.git

# Asks only for password and then fatal: Authentication failed for '...'
git clone https://git@gitlab.com/group/repo.git

# fatal: repository '...' does not exist
git clone git@gitlab.com/group/repo  
git clone git@gitlab.com/group/repo.git 
git clone <myGitLabUser>@gitlab.com/group/repo
git clone <myGitLabUser>@gitlab.com/group/repo.git

What am I doing wrong? Is this not possible?

Paloha
  • 558
  • 6
  • 14
  • just in case - are there any chances you have configured gitlab to be accessible via any specific port? in this case you'll have to also specify port – Fyodor Volchyok Dec 07 '21 at 18:04
  • No, no custom port. – Paloha Dec 07 '21 at 18:06
  • 1
    aren't you supposed to put ssh:// scheme before path to repo? like "git clone ssh://git@gitlab.com/group/repo.git" ? – Fyodor Volchyok Dec 07 '21 at 18:09
  • @FyodorVolchyok wow, this worked. Thank you! Would you add it as an answer so I can accept it? Btw, after using the https method I was also not able to git push. It asked the username and password and then returned error. I figured out the problem was in `git remote` which was set to `origin https://gitlab.com/group/repo` instead of `origin git@gitlab.com:group/repo.git`. I changed it using https://stackoverflow.com/a/2432799/8691571 and all works like a charm now. – Paloha Dec 07 '21 at 18:27
  • 1
    glad it helped :) not gonna lie - all this ssh:// stuff is always a PITA with paths, keys, config, etc. – Fyodor Volchyok Dec 07 '21 at 18:35
  • I have just figured out that this command works as expected even without the `ssh`: `git clone git@gitlab.com:group/repo.git`. Actually, I deciphered it from somewhat cryptic line in the official docs: `git clone git@:gitlab-org/gitlab.git`. I guess this should be the accepted answer, since it is in line with the docs. If you edit your answer to contain this I will accept it. You can also keep both methods maybe. It is up to you. – Paloha Dec 07 '21 at 18:36
  • that's interesting. I just checked one of my local repos - it has ssh:// scheme. attempt to clone it without ssh:// fails – Fyodor Volchyok Dec 07 '21 at 18:39
  • Please, note the `:` after `git@gitlab.com` instead of `/`. Does it not work even with `:`? – Paloha Dec 07 '21 at 18:41
  • indeed, after some trials it worked. it turns out it's called "scp-like" syntax https://stackoverflow.com/a/16134428/2305175 . you're right :) As for my answer - I don't think it holds too much value. maybe we should even close this question as a duplicate of https://stackoverflow.com/a/16134428/2305175. What do you think? – Fyodor Volchyok Dec 07 '21 at 18:57
  • I hoped this question would be more friendly to google. My google-fu did not bring me to the question you link. So maybe if you summarize it in your answer (along with the link to that question) such that others have easy time finding a solution I can give you the accepted answer for the effort. – Paloha Dec 07 '21 at 19:31
  • I agree, the question I mentioned formulated in different way and isn't narrow enough to emphasize url syntax. I've updated my answer to reflect what we've found – Fyodor Volchyok Dec 07 '21 at 19:46

1 Answers1

1

It appears that according to this answer you could choose between two types of urls

  • either with schema, ssh://git@gitlab.com/group/repo.git in this case
  • or "scp-like", without schema, git@gitlab.com:group/repo.git (example from gitlab docs)

You have to choose only one of them and mixing them will most likely lead to problems (that's what happened with ssh urls at question).

Also note that if you prefer scp-like urls AND you have to specify port you probably want to put port into .ssh/config file instead of url, like this (I personally failed to figure out correct port placement directly in url but config file worked without issues).

Fyodor Volchyok
  • 5,610
  • 4
  • 28
  • 45
  • To avoid confusion, the "scp-like" syntax is: `git@gitlab.com:group/repo.git`. The syntax with `user_1` is specific to the situation when you [use different accounts on a single GitLab instance](https://docs.gitlab.com/ee/ssh/#use-different-accounts-on-a-single-gitlab-instance). Since this is not standard, maybe you could edit your question, because this is what confused me in the docs in the first place. Thx – Paloha Dec 08 '21 at 11:12