Why git fetch is not using the same keys as ssh -T?

Viewed 34

I am working on windows 10 and I am trying to connect git via ssh with selfhosted(corporate) instance of gitlab. I try to follow guide here https://docs.gitlab.com/ee/ssh/. I am at the stage that ssh -Tv git@corpo.gitlab correctly logs me into the instance, however when I try to do git fetch gitlab ssh connection fails.

I have tried to compare debug output of standalone ssh invocation and git invoked ssh, and I have found out that in "git" variant, my private key is not even tried to be used. Logs says it looks in the correct user directory (so it is not an 'user scope' isse).

Why this can happen? I have added remote to the git repository via git remote gitlab git@corp.gitlab.

setting proper ssh config file with

 Host ssh.corpo.gitlab
     HostName ssh.corpo.gitlab
     IdentityFile ~/.ssh/my_corpo_key

fixes the issue, but I think it should work without those additional settings. What could make the difference ?

1 Answers

I have added remote to the git repository via git remote gitlab git@corp.gitlab.

If you want to use your key, you would need a ~/.ssh/config with:

Host corpo.gitlab
  Hostname ssh.corpo.gitlab
  IdentityFile ~/.ssh/my_corpo_key
  User git

And the URL would become:

git remote set-url gitlab corpo.gitlab:<me>/<myrepo>
                          ^^^^^^^^^^^^
                   (need to match the Host entry)
Related