Managing multiple git account

balaaagi

Balaji Srinivasan

Posted on May 24, 2020

Managing multiple git account

Lot of companies requests developers to create dedicated git accounts to access work related git projects. It is good from security and access control perspective but it is a pain from developers who wish to work on personal projects in their private accounts. Lot of times commits goes in the name of work accounts or the otherway. I have also gone through this that is why have setup my work laptop as below such that there is clear segregation of usage of git accounts

Prerequisite

  • You should be using ssh based git access which most of the devs would be using. I still find still few are using http based and giving creds each time šŸ¤¦ā€ā™‚ļø
  • You should be used to segregate your office work space (code folders) from your private workspace

Folder Structure

I usually go with the below folder structures. This has helped me in maintaining a clean structure and managing multiple git accounts

ā”œā”€ā”€ personal
ā”‚Ā Ā  ā””ā”€ā”€ workspace
ā”‚Ā Ā      ā”œā”€ā”€ repo1
ā”‚Ā Ā      ā””ā”€ā”€ repo2
ā””ā”€ā”€ work
    ā””ā”€ā”€ workspace
        ā”œā”€ā”€ workrepo1
        ā””ā”€ā”€ workrepo2
Enter fullscreen mode Exit fullscreen mode

SSH Keys

Ensure you create two different ssh keys for each of your git accounts. Please refer this link.
Once you have the keys ensure to follow this ensure to configure the corresponding Github account with corresponding keys. Follow this link.
Since you have generated two keys two private key files would be present.

SSH Configuration

Since we have two different ssh keys we need to configure it to use it. Below are the ssh configs

Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/<private key filename of private account>

Host <githubwork.com>    
   HostName github.com
   User git
   IdentityFile ~/.ssh/<private key filename of work account>
Enter fullscreen mode Exit fullscreen mode

Git Configuration

Here I will be setting the default git config as my private account and configure work git account specific to work associated directories. But we can do the other way based on how we are structuring our workspace.
Below is configuration in default git config in /.gitconfig

[user]
    name = Personal Git Account Name
    email = personalemailassociatedwithpersonalgit@somedomain
Enter fullscreen mode Exit fullscreen mode

The above will ensure all the git operations uses the above config across your machine.

Work Related Git Configuration

In the same ~/.gitconfig add these lines.

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig
Enter fullscreen mode Exit fullscreen mode

What we have done is used the includeif.Conditional Includes was introduced from git 2.13. Ensure you have this version of git to use this feature. It actually includes gitconfig dynamically based on the conditions. In this case if the git working directory is ~/work it uses the .gitconfig from the ~/work/.gitconfig.
Below are the contents of this config file inside ~/work/.gitconfig

[user]
    name = Work Git Account Name
    email = workacountemail@domain
Enter fullscreen mode Exit fullscreen mode

How to Work on repositories from Both Account

  • To clone a repo from work account you need to follow this since you have configured the ssh - git clone git@githubwork.com:<repopath>.git
  • To clone a repo from private account you need to follow this since you have configured the ssh - git clone git@github.com:<repopath>.git

All the git operations on corresponding repos ensure to follow the corresponding configs. You can customize more configurations in this custom git configs.

šŸ’– šŸ’Ŗ šŸ™… šŸš©
balaaagi
Balaji Srinivasan

Posted on May 24, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

"What the hell were they thinking?"
github "What the hell were they thinking?"

February 17, 2023

You Must use these Git cmds
github You Must use these Git cmds

May 29, 2021

Most useful aliases for git
github Most useful aliases for git

May 28, 2021

Managing multiple git account
github Managing multiple git account

May 24, 2020