DEV Community

Subhash Halder
Subhash Halder

Posted on • Originally published at subhashhalder.com

How to Manage Multiple GitHub Repositories from a Single Machine

Problem

Many developers use their working laptop to contribute to open-source projects or personal projects. However, it can be a pain to set up a GitHub email for every repository. Sometimes we forget to update the email and contribute to a personal repository with our work email, or vice versa. Today I will show a simple solution to this problem.

Let say I am working in an Organization with GitHub username as subhash-xyz and my personal GitHub username is subhash-halder, first thing I do is set two different base folder for two username (you can use it for multiple account also).

# Organization projects base path
$ ~/Documents/work/xyz/
# Personal projects base path
$ ~/Documents/work/personal/
Enter fullscreen mode Exit fullscreen mode

Create two different ssh keys

Now we need to create two different ssh keys for two different account.

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

This is the basic command to create ssh key, here we have to run the command two time and save the key in two different files. For more details of ssh key creation please visit this link.

I have created two ssh file as follows

$ ~/.ssh/github_xyz
$ ~/.ssh/github_personal
Enter fullscreen mode Exit fullscreen mode

Now we have to add the public key to the respective GitHub Account.

Create a global .gitconfig

Now create a .gitconfig file in you home directory and paste the following code, please update it according to your need.

# ~/.gitconfig
[includeIf "gitdir:~/Documents/work/personal/"]
  path = ~/.gitconfig-personal
[includeIf "gitdir:~/Documents/work/xyz/"]
  path = ~/.gitconfig-xyz
Enter fullscreen mode Exit fullscreen mode

This tells Git if the git folder is inside these folder use the respective config files.

Create the folder specific git configuration

Since we have specified that we will use different configuration file for different folder now we need to create them.

~/.gitconfig-personal

[user]
  name = Subhash Halder
  email = halder.subhash@gmail.com

[github]
  user = "subhash-halder"

[core]
  sshCommand = "ssh -i  ~/.ssh/personal_github"
Enter fullscreen mode Exit fullscreen mode

~/.gitconfig-xyz

[user]
  name = Subhash Halder
  email = halder.subhash@xyz.com

[github]
  user = "subhash-xyz"

[core]
  sshCommand = "ssh -i  ~/.ssh/personal_xyz"
Enter fullscreen mode Exit fullscreen mode

You can now use git command without worry

# If I run git clone command from personal folder it will use my personal ssh key
~/Documents/work/personal $ git clone git@github.com:subhash-halder/subhashhalder.com.git
~/Documents/work/personal $ cd subhashhalder.com
# If you run git command from inside the git folder it will show the config from the local configuration file.
~/Documents/work/personal/subhashhalder.com $ git config user.email
~/Documents/work/personal/subhashhalder.com $ halder.subhash@gmail.com
# If you run git command from inside the git folder of your Organization dir it will show the config from the local configuration file.
~/Documents/work/xyz $ git clone git@github.com:xyz/demo.git
~/Documents/work/xyz $ cd demo
~/Documents/work/xyz/demo $ git config user.email
~/Documents/work/xyz/demo $ halder.subhash@xyz.com
Enter fullscreen mode Exit fullscreen mode

Top comments (0)