<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Blog on Vousten.dev</title><link>https://vousten.dev/blog/</link><description>Recent content in Blog on Vousten.dev</description><generator>Hugo -- gohugo.io</generator><language>en-gb</language><lastBuildDate>Wed, 04 Sep 2024 13:32:20 +0200</lastBuildDate><atom:link href="https://vousten.dev/blog/index.xml" rel="self" type="application/rss+xml"/><item><title>How to use multiple remote git Instances</title><link>https://vousten.dev/blog/2022/02/how-to-use-multiple-remote-git-instances/</link><pubDate>Fri, 04 Feb 2022 22:00:00 +0100</pubDate><guid>https://vousten.dev/blog/2022/02/how-to-use-multiple-remote-git-instances/</guid><description>&lt;img src="https://vousten.dev/blog/2022/02/how-to-use-multiple-remote-git-instances/Git-Logo-1788C.png" alt="Featured image of post How to use multiple remote git Instances" />&lt;h2 id="gits-global-config">Gits global config
&lt;/h2>&lt;p>When you create an empty git repository on one of the remote hosting platforms like GitLab, you usually get some setup commands displayed.
Those are intended to help you set up your environment pretty quickly, but there are two commands which can screw things up for other remote hosts.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">git config --global user.name &lt;span class="s2">&amp;#34;User&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">git config --global user.email &lt;span class="s2">&amp;#34;user@mail-domain&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>These commands should configure your username and email for the specific git account on the remote host to link the commits to your account.
But using multiple remote Instances with different credentials leads to problems.&lt;br>
After running those two commands, you have globally set your credentials.
For every git repository, those credentials will be used if they are not locally overwritten.&lt;/p>
&lt;p>You could set those credentials as your global config if you want to use your university&amp;rsquo;s remote git Instance for multiple projects.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">git config --global user.name &lt;span class="s2">&amp;#34;UniversityID&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">git config --global user.email &lt;span class="s2">&amp;#34;UniversityID@uni-domain&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>But these settings will immediately screw up your other Instance because those are set globally and will overwrite the previous one.
The following commit you will make on the public server will be linked to your university account.&lt;/p>
&lt;h2 id="solution-use-local-config">Solution: Use local config?
&lt;/h2>&lt;p>Just remove the &lt;code>--global&lt;/code> option to fix this, right?&lt;/p>
&lt;p>Not really, because now you must do this for every repository you are working on once.
Git will display a message that you must set credentials if no global settings are configured.&lt;/p>
&lt;p>If you use these Instances once, the solution is to set the credentials for the usual Instance globally and then set the others locally.
So when you commit now, you will use your regular username and email because they are global.
The local configs will be used when creating a commit in the repository for your &amp;lsquo;single-use&amp;rsquo; Instance because they will temporarily overwrite the global settings.&lt;/p>
&lt;p>But if you, like me, use multiple remote Instances regularly, always setting the local config can get tedious pretty fast.&lt;/p>
&lt;h2 id="gitconfig">Gitconfig
&lt;/h2>&lt;p>So let&amp;rsquo;s take a look at the gitconfig.&lt;/p>
&lt;p>This config is usually located in your user directory under the name &lt;code>.gitconfig&lt;/code>, whether you are on Windows or Linux.
I can&amp;rsquo;t confirm this for Mac, but it might be similar.&lt;br>
If you used the first two commands to set your credentials globally, the gitconfig would contain an entry similar to this:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-properties" data-lang="properties">&lt;span class="line">&lt;span class="cl">&lt;span class="err">[user]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">name&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s">User&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">email&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s">user@mail-domain&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>Those credentials will be used for every commit if they are not overwritten in the local config, which you can find under &lt;code>.git/config&lt;/code> inside the repository.
It will have the same user sections but with different usernames and emails.&lt;/p>
&lt;p>So the next step is to change the config so that the correct username and email will be used for the chosen Instance.&lt;/p>
&lt;h3 id="extending-the-config">Extending the config
&lt;/h3>&lt;p>My solution to this problem is to use the &lt;code>includeIf&lt;/code> keyword inside the gitconfig.
To make this work, I have a folder with all my repositories on my system.
Inside this folder, I create subdirectories named after every remote host, so your directory structure may look something like this:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-console" data-lang="console">&lt;span class="line">&lt;span class="cl">&lt;span class="go">Git/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">├─ github.com/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">├─ gitlab.com/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">├─ vousten.dev/
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>So, for one Instance, all the repositories are located in the folder named after this Instance.&lt;/p>
&lt;p>Next, you create multiple separate gitconfigs for those Instances.
I created a new folder inside the user directory called &lt;code>.gitconfigs&lt;/code>.
The dot will hide the folder by default and has a similar name to the original config.
Inside this folder, I create new files named after those Instances, so it looks like this:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-console" data-lang="console">&lt;span class="line">&lt;span class="cl">&lt;span class="go">.gitconfigs/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">├─ github.com
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">├─ gitlab.com
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">├─ vousten.dev
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>I add, or better yet, create, a user section inside those files, just like in the global config, and enter my credentials for the specific Instance.
You could add other settings if you want them to be used only for the particular host.&lt;/p>
&lt;p>To tell git that it should use these settings, add the following inside the global &lt;code>.gitconfig&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-properties" data-lang="properties">&lt;span class="line">&lt;span class="cl">&lt;span class="na">[includeIf&lt;/span> &lt;span class="s">&amp;#34;gitdir:Git/github.com/**&amp;#34;]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">path&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s">~/.gitconfigs/github.com&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="na">[includeIf&lt;/span> &lt;span class="s">&amp;#34;gitdir:Git/gitlab.com/**&amp;#34;]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">path&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s">~/.gitconfigs/gitlab.com&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="na">[includeIf&lt;/span> &lt;span class="s">&amp;#34;gitdir:Git/vousten.dev/**&amp;#34;]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">path&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s">~/.gitconfigs/vousten.dev&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>These config settings mean the specific Instance config will only be used if you use git in the host directory you created previously.
So for every repository inside the &lt;code>Git/gitlab.com/&lt;/code> directory, the config &lt;code>.gitconfigs/gitlab.com&lt;/code> is temporarily included in the global gitconfig.&lt;/p>
&lt;p>Now you always have the proper credentials for every remote host unless you are working in the wrong directory, which may be the only downside of this solution.
So you have to structure your repository thoughtfully to use this.&lt;/p>
&lt;h2 id="multiple-ssh-keys">Multiple SSH Keys
&lt;/h2>&lt;p>This step is not needed to have the automatic credential system running, but I personally like it to have different SSH keys for different Instances.
So howsoever you get your private key stolen, for one Instance, the other ones are still safe.&lt;/p>
&lt;p>To do this, you must first create multiple SSH keys and store them inside &lt;code>.ssh\&lt;/code> located in the user directory.
It is the same for Windows and Linux, like before.
I created an extra directory for all my git keys inside this directory to have a bit of order in my file system.&lt;/p>
&lt;p>The command to create an SSH key for one Instance could look something like this:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-console" data-lang="console">&lt;span class="line">&lt;span class="cl">&lt;span class="go">ssh-keygen -t ed25519 -f ~/.ssh/git/git.vousten.dev
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>You have to edit the config file to select which key should be used.&lt;/p>
&lt;p>Assuming your file structure looks something like this:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;span class="lnt">7
&lt;/span>&lt;span class="lnt">8
&lt;/span>&lt;span class="lnt">9
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-console" data-lang="console">&lt;span class="line">&lt;span class="cl">&lt;span class="go">.ssh/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">├─ config
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">├─ git/
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">│ ├─ github.com
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">│ ├─ github.com.pub
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">│ ├─ gitlab.com
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">│ ├─ gitlab.com.pub
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">│ ├─ git.vousten.dev
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="go">│ ├─ git.vousten.dev.pub
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>You then have to add new hosts to the config file so that ssh knows what key it should use.
The resulting config file for my case looks like this:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-properties" data-lang="properties">&lt;span class="line">&lt;span class="cl">&lt;span class="na">Host&lt;/span> &lt;span class="s">git.vousten.dev&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">IdentityFile&lt;/span> &lt;span class="s">~/.ssh/git/git.vousten.dev&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="na">Host&lt;/span> &lt;span class="s">github.com&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">IdentityFile&lt;/span> &lt;span class="s">~/.ssh/git/github.com&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="na">Host&lt;/span> &lt;span class="s">gitlab.com&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="na">IdentityFile&lt;/span> &lt;span class="s">~/.ssh/git/gitlab.com&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>If you connect now to a git host, the system will use the specified key, and thus you have a different key pair for every Instance.&lt;/p>
&lt;h2 id="conclusion">Conclusion
&lt;/h2>&lt;p>The setup is straightforward and solves the problem of different credentials on different remote hosts.
There may be even better solutions, but this is the one I like to use, and that works great for me.
You may need to adapt it to your style or even search the internet, like me, if this will not work for your coding environment.&lt;br>
But I hope this gives you an idea for a custom solution, and as you see, this whole config is not complex, so that you can find an adaption to your system pretty quickly.&lt;/p></description></item></channel></rss>