<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Soham Mondal</title>
    <description>The latest articles on DEV Community by Soham Mondal (@sohammondal).</description>
    <link>https://dev.to/sohammondal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F214163%2F70f3a675-a9a0-46d2-86ad-78fe7758ca89.jpeg</url>
      <title>DEV Community: Soham Mondal</title>
      <link>https://dev.to/sohammondal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sohammondal"/>
    <language>en</language>
    <item>
      <title>How to setup 2 separate Git profiles on a single device?</title>
      <dc:creator>Soham Mondal</dc:creator>
      <pubDate>Thu, 09 Apr 2020 16:27:26 +0000</pubDate>
      <link>https://dev.to/sohammondal/how-to-setup-2-separate-git-profiles-on-a-single-device-5b95</link>
      <guid>https://dev.to/sohammondal/how-to-setup-2-separate-git-profiles-on-a-single-device-5b95</guid>
      <description>&lt;p&gt;I work on a single device and use it for both my office work and side/pet/personal projects. Default git credentials for the device is obviously the one provided by my office. Now, this scenario has happened to me a lot of times - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;My office work is done&lt;/li&gt;
&lt;li&gt;I am super excited about a personal project idea&lt;/li&gt;
&lt;li&gt;So I set up a remote git repository&lt;/li&gt;
&lt;li&gt;Set up the project boilerplate locally&lt;/li&gt;
&lt;li&gt;Fire &lt;code&gt;git init&lt;/code&gt;, &lt;code&gt;git remote add&lt;/code&gt;, &lt;code&gt;git add&lt;/code&gt;, &lt;code&gt;git commit&lt;/code&gt; &amp;amp; PUSH!!!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.tenor.com%2Fimages%2F06655070b3cc8faaff4824eee848adc0%2Ftenor.gif%3Fitemid%3D5436796" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.tenor.com%2Fimages%2F06655070b3cc8faaff4824eee848adc0%2Ftenor.gif%3Fitemid%3D5436796" alt="facepalm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And it is then when I realize I have pushed code to my personal git with wrong (office) credentials. And I start all over again. &lt;/p&gt;

&lt;p&gt;Do let me know if you have ever faced a similar situation like mine in comments!&lt;/p&gt;

&lt;p&gt;So, now what is the solution? How do I have separate git config on a single machine? The answer is pretty simple. You need to have two separate Git config files. But, how ? Read on...&lt;/p&gt;

&lt;p&gt;*** &lt;b&gt;Before starting make sure you have git version 2.13 or higher installed in your system.&lt;/b&gt; ***&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git &lt;span class="nt"&gt;--version&lt;/span&gt;
git version 2.14.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now with the prerequisites set, let's begin -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your &lt;code&gt;.gitconfig&lt;/code&gt; resides in your home directory. For Windows users, it will be in the path &lt;code&gt;C:\Users\[YOUR_NAME]&lt;/code&gt;. Go to that directory.&lt;/li&gt;
&lt;li&gt;Create two separate folders like &lt;em&gt;My_Office_Workspace&lt;/em&gt; &amp;amp; &lt;em&gt;My_Personal_Workspace&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Create two separate &lt;code&gt;.gitconfig&lt;/code&gt; files, one for office and another for personal usage in the path &lt;code&gt;C:\Users\[YOUR_NAME]&lt;/code&gt;. Let's name them &lt;code&gt;.gitconfig-office&lt;/code&gt; &amp;amp; &lt;code&gt;.gitconfig-personal&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The contents of &lt;code&gt;.gitconfig-office&lt;/code&gt; -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# This is Git's per-user configuration file.&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;user]
name &lt;span class="o"&gt;=&lt;/span&gt; YOUR_OFFICE_GIT_USERNAME
email &lt;span class="o"&gt;=&lt;/span&gt; your.name@company.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The contents of &lt;code&gt;.gitconfig-personal&lt;/code&gt; -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# This is Git's per-user configuration file.&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;user]
name &lt;span class="o"&gt;=&lt;/span&gt; YOUR_PERSONAL_GIT_USERNAME
email &lt;span class="o"&gt;=&lt;/span&gt; your.name@gmail.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;After that's done, open up the &lt;code&gt;.gitconfig&lt;/code&gt; file, and make the necessary changes. It should look like -
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;includeIf &lt;span class="s2"&gt;"gitdir:~/My_Office_Workspace/"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  path &lt;span class="o"&gt;=&lt;/span&gt; .gitconfig-office
&lt;span class="o"&gt;[&lt;/span&gt;includeIf &lt;span class="s2"&gt;"gitdir:~/My_Personal_Workspace/"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  path &lt;span class="o"&gt;=&lt;/span&gt; .gitconfig-personal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configuration done! Now let's verify.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Goto &lt;em&gt;My_Office_Workspace&lt;/em&gt; and clone a project from your office GitHub, BitBucket or GitLab. Let's call the project &lt;em&gt;My_Office_Project&lt;/em&gt; and do the following -
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;My_Office_Workspace/
&lt;span class="nv"&gt;$ &lt;/span&gt;git clone https://github.my_office.com/VeryImportant/My_Office_Project.git
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;My_Office_Project/
&lt;span class="nv"&gt;$ &lt;/span&gt;git config user.name
YOUR_OFFICE_GIT_USERNAME
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can also do the same for your personal projects under &lt;em&gt;My_Perosonal_Workspace&lt;/em&gt;, but you will see you the output for the last command &lt;code&gt;git config user.name&lt;/code&gt; is YOUR_PERSONAL_GIT_USERNAME.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.tenor.com%2Fimages%2Fbff89595f10f1b07608dbe71ca67e75e%2Ftenor.gif%3Fitemid%3D4185058" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.tenor.com%2Fimages%2Fbff89595f10f1b07608dbe71ca67e75e%2Ftenor.gif%3Fitemid%3D4185058" alt="success"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Woohoo, it's done! No more messing up. 😃&lt;/p&gt;

&lt;h3&gt;
  
  
  Update
&lt;/h3&gt;

&lt;p&gt;If your projects are in a different drive on Windows, for eg. D or F, then you can configure the &lt;code&gt;.gitconfig&lt;/code&gt; like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;includeIf &lt;span class="s2"&gt;"gitdir:D:/My_Office_Workspace/"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  path &lt;span class="o"&gt;=&lt;/span&gt; .gitconfig-office
&lt;span class="o"&gt;[&lt;/span&gt;includeIf &lt;span class="s2"&gt;"gitdir:F:/My_Personal_Workspace/"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  path &lt;span class="o"&gt;=&lt;/span&gt; .gitconfig-personal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Liked this post? Share it ↩️ or maybe give a heart 🧡. &lt;/p&gt;

&lt;p&gt;I am &lt;a href="https://sohammondal.com" rel="noopener noreferrer"&gt;Soham Mondal&lt;/a&gt;, a developer from India. You can follow me on &lt;a href="https://www.linkedin.com/in/soham-sm-mondal/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;, &lt;a href="https://twitter.com/sohammondal_" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;, or &lt;a href="https://github.com/sohammondal/" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. You can learn more about me on my &lt;a href="https://sohammondal.com/about" rel="noopener noreferrer"&gt;website&lt;/a&gt;. I am currently open to new projects &amp;amp; job offers.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>bash</category>
      <category>hack</category>
    </item>
  </channel>
</rss>
