How to access a remote Git repository in Jenkins using aliases?
<p><strong>short version</strong></p>

<p>I'm using aliasing to connect my remote Git repositories. It works, but when I add such repository to Jenkins, it cannot access the repository and display the error:</p>

<blockquote>
  <p>Failed to connect to repository : Command "git -c core.askpass=true
  ls-remote -h git@github.com-foo:myaccount/myrepository.git HEAD"
  returned status code 128: stdout: stderr: ssh: Could not resolve
  hostname github.com-foo: Name or service not known fatal: Could not
  read from remote repository.</p>
  
  <p>Please make sure you have the correct access rights and the repository
  exists.</p>
</blockquote>

<hr>

<p><strong>detailed issue description</strong></p>

<p>In order to <code>commit</code>/<code>push</code> code as different users I use aliasing:</p>

<p><strong><code>C:\Users\myusername\.ssh\config</code></strong></p>

<pre><code>...

#github.com-foo account
Host github.com-foo
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_foo

#github.com-bar account
Host github.com-bar
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_bar

...
</code></pre>

<p>Then instead of</p>

<pre><code>git clone git@github.com:myaccount/myrepository.git
</code></pre>

<p>I simply use</p>

<pre><code>git clone git@github.com-foo:myaccount/myrepository.git
</code></pre>

<p>The <code>.ssh</code> directory is accessible from my local VMs as a shared folder, so also use my credentials on the VMs as well.</p>

<p>Everything works.</p>

<p>Now I installed Jenkins on a VM and want to configure the connection to a remote repository stored on GitHub. What I did:</p>

<ol>
<li><p>Credentials: <code>Jenkins -&gt; Credentials -&gt; [link] Global credentials (unrestricted) -&gt; [radio button] Enter directly -&gt; [textarea] Key -&gt; added -&gt; [button] Save</code>.</p></li>
<li><p>Git plugin: <code>Jenkins -&gt; Configuration -&gt; [section] Git plugin</code> -- Email and Name set up:</p></li>
</ol>

<p><img src="https://i.stack.imgur.com/ZWpXu.png" alt="enter image description here"></p>

<ol start="3">
<li>Project Git settings: <code>[project] -&gt; Configuration -&gt; [section] Source Code Management -&gt; [section] Git</code> -- Repository URL and credentials:</li>
</ol>

<p><img src="https://i.stack.imgur.com/lruTi.png" alt="enter image description here"></p>

<p>As on the screenshot shown, the connection is failing.</p>

<p><strong>Why does it not work and how to get it working, in order to connect a remote Git repository in Jenkins using aliases?</strong></p>