DEV Community

Balaji Srinivasan
Balaji Srinivasan

Posted on • Originally published at blog.balaaagi.in

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.

Top comments (4)

Collapse
 
kes777 profile image
Eugen Konkov

I still find still few are using http based and giving creds each time πŸ€¦β€β™‚οΈ
This is not required, because those can be configured as:

git remote -v
origin  https://username:password@domain/path/repo.git (fetch)
Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Or just use something like GitKraken - makes this an absolute breeze

Collapse
 
heinekeg profile image
Heinekeg

+1 for GitKraken, but this is useful for those that don't use a GUI

Collapse
 
codewithfrida profile image
Frida~πŸ‘©β€πŸ’»

Wow, exactly what I was looking for. Just yesterday I tried to connect two GitHub accounts and failed miserably ;-) Thank you for sharing!