<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" 
  xmlns:content="http://purl.org/rss/1.0/modules/content/" 
  xmlns:dc="http://purl.org/dc/elements/1.1/" 
  xmlns:atom="http://www.w3.org/2005/Atom" 
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" 
  xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>git on Solid Soft</title>
    <link>https://blog.solidsoft.pl/tags/git/</link>
    <description>Recent content in git on Solid Soft</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <copyright>©{year}, All Rights Reserved</copyright>
    <lastBuildDate>Tue, 27 Aug 2019 00:00:00 +0000</lastBuildDate>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>2</sy:updateFrequency>
    
        <atom:link href="https://blog.solidsoft.pl/tags/git/index.xml" rel="self" type="application/rss+xml" />
    
    
    

      
      <item>
        <title>Auto-switchable multiple Git identities on one computer - Git Tricks #1</title>
        <link>https://blog.solidsoft.pl/2019/08/27/auto-switchable-multiple-git-identities-on-one-computer-git-tricks-1/</link>
        <pubDate>Tue, 27 Aug 2019 00:00:00 +0000</pubDate>
        
        <atom:modified>Tue, 27 Aug 2019 00:00:00 +0000</atom:modified>
        <guid>https://blog.solidsoft.pl/2019/08/27/auto-switchable-multiple-git-identities-on-one-computer-git-tricks-1/</guid>
        <description>Have you even committed to Git using wrong email address working on/for different projects/companies? Luckily with a little configuration Git can auto-switch the identities for you.
 (Too long) introduction and reasoning Being an (experienced) IT professional can give you an opportunity to work on different things in the same time frame. For instance, in addition to work for the main client, I do some consultancy stuff with code quality &amp;amp; testing and Continuoues Delivery for other companies.</description>
        <content:encoded>&lt;blockquote&gt;
&lt;p&gt;Have you even committed to Git using wrong email address working on/for different projects/companies? Luckily with a little configuration Git can auto-switch the identities for you.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&#34;too-long-introduction-and-reasoning&#34;&gt;(Too long) introduction and reasoning&lt;/h2&gt;
&lt;p&gt;Being an (experienced) IT professional can give you an opportunity to work on different things in the same time frame. For instance, in addition to work for the main client, I do some consultancy stuff with code quality &amp;amp; testing and Continuoues Delivery for other companies. What&amp;rsquo;s more, I also conduct training sessions (will a lot of exercises with code) and work on my own and other&amp;rsquo;s FOSS projects. Usually, it is convenient to do it on the same computer. It can happen to commit something with the wrong email address (to be detected later on by an external auditor ;-) ) and a push with force to a remote master after rebase is not the best possible solution :-). I started with some &lt;a href=&#34;https://fishshell.com/&#34; target=&#34;_blank&#34;&gt;Fish&lt;/a&gt;
-based script to deal with it, but in the end I have found a built-in mechanism in Git itself.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;. This particular blog post doesn&amp;rsquo;t treat about anything new or revolutionary. However, I&amp;rsquo;ve been living in unawareness long enough to give my blog readers a chance to know that mechanism right now.&lt;/p&gt;


&lt;figure &gt;
  
    &lt;img src=&#34;https://blog.solidsoft.pl/images/posts/2019/superhero-git-identities.jpg&#34;  style=&#34;width:;height:;&#34;/&gt;
  
  
&lt;/figure&gt;

&lt;h2 id=&#34;project-situation&#34;&gt;Project situation&lt;/h2&gt;
&lt;p&gt;My &lt;code&gt;~/Code&lt;/code&gt; directory structure could be simplified to something like that:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Code/
├── gradle-nexus-staging-plugin
├── mockito-java8
├── spock-global-unroll
├── ...
├── external-foss
│   ├── ...
│   ├── awaitility
│   └── spock
├── training
│   ├── ...
│   ├── code-testing
│   ├── java11
│   └── jenkins-as-code
└── work
    ├── ...
    ├── codearte
    └── companyX
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The goal is to keep an email address in the company&amp;rsquo;s projects appropriate.&lt;/p&gt;
&lt;h2 id=&#34;solution&#34;&gt;Solution&lt;/h2&gt;
&lt;p&gt;The first idea which can spring to your mind is to manually override &lt;code&gt;git config user.email &amp;quot;...&amp;quot;&lt;/code&gt; in every clonned company repository. However, it&amp;rsquo;s tedious and error prone (especially in the microservice-based architecture :) ).&lt;/p&gt;
&lt;p&gt;Luckily, one of the features introduces in &lt;a href=&#34;https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.13.0.txt&#34; target=&#34;_blank&#34;&gt;Git 2.13.0&lt;/a&gt;
 (long time ago - May 2017) is a conditional configuration applying (aka &lt;a href=&#34;https://git-scm.com/docs/git-config#_includes&#34; target=&#34;_blank&#34;&gt;Conditional includes&lt;/a&gt;
). Armed with it our task is pretty simple.&lt;/p&gt;
&lt;p&gt;First, keep your default name and email defined in the &lt;code&gt;[user]&lt;/code&gt; section in &lt;code&gt;~/.gitconfig&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-cfg&#34; data-lang=&#34;cfg&#34;&gt;&lt;span class=&#34;k&#34;&gt;[user]&lt;/span&gt;
  &lt;span class=&#34;na&#34;&gt;name&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;Marcin Zajączkowski
&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;  email = foss.hacker@mydomain.example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Next, create a company related config file in the home directory, e.g. &lt;code&gt;~/.gitconfig-companyX&lt;/code&gt; with just overriden email address:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-cfg&#34; data-lang=&#34;cfg&#34;&gt;&lt;span class=&#34;k&#34;&gt;[user]&lt;/span&gt;
  &lt;span class=&#34;na&#34;&gt;email&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;marcin.zajaczkowski@companyx.example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Glue it together by placing in &lt;code&gt;~/.gitconfig&lt;/code&gt; the following code:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-cfg&#34; data-lang=&#34;cfg&#34;&gt;&lt;span class=&#34;k&#34;&gt;[includeIf &amp;#34;gitdir:~/Code/work/companyX/&amp;#34;]&lt;/span&gt;
  &lt;span class=&#34;na&#34;&gt;path&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;.gitconfig-companyX&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The &lt;code&gt;.gitconfig-companyX&lt;/code&gt; is applied only if a current path prefix matches the one defined in &lt;code&gt;includeIf&lt;/code&gt;. Simple, yet powerful. That mechanism can be also used to configure different things - such as conditionally using GPG to sign your commits.&lt;/p&gt;
&lt;p&gt;Btw, being a paranoid you can even remove the email field from the root configuration to be notified if you forgot to add an email for any new company you colaborate with.&lt;/p&gt;
&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;
&lt;p&gt;Thanks to the &lt;code&gt;includeIf&lt;/code&gt; mechanism you will never (*) again commit to a repo with wrong name or email. That and other Git related tricks and commands/aliases (such as collected in &lt;a href=&#34;https://github.com/szpak/gitkurka/&#34; target=&#34;_blank&#34;&gt;git-kurka&lt;/a&gt;
 make it easier to focus on delivering the stuff :).&lt;/p&gt;
&lt;p&gt;Feel free to leave your favorite Git tips &amp;amp; tricks in the comments.&lt;/p&gt;
&lt;p&gt;Update. From the comment made on priv:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Git works great in that case, but for more generic changes in the environment variables &lt;a href=&#34;https://mobile.twitter.com/wybczu&#34; target=&#34;_blank&#34;&gt;Wybczu&lt;/a&gt;
 suggested very powerful &lt;a href=&#34;https://direnv.net/&#34; target=&#34;_blank&#34;&gt;direnv&lt;/a&gt;
 (with support for bash, zsh, tcsh, fish and elvish)&lt;/li&gt;
&lt;/ul&gt;
&lt;br&gt;
&lt;span style=&#34;opacity: 0.6&#34;&gt;The lead photo based on the &lt;a href=&#34;https://pixabay.com/users/EliasSch-3372715/&#34;&gt;Elias Sch&lt;/a&gt; &amp;lsquo;s work published in &lt;a href=&#34;https://pixabay.com/&#34;&gt;Pixabay&lt;/a&gt;, Pixabay License.&lt;/span&gt;
</content:encoded>
        <dc:creator>Marcin Zajączkowski</dc:creator>
        <media:content url="https://blog.solidsoft.pl/images/posts/2019/superhero-git-identities.jpg" medium="image"><media:title type="html">featured image</media:title></media:content>
        
        
        
          
            
              <category>git</category>
            
          
            
              <category>git-tricks</category>
            
          
            
              <category>git-kurka</category>
            
          
        
        
          
            
              <category>Tricks &amp; Tips</category>
            
          
        
        
      </item>
      

    
  </channel>
</rss>