How to Manage Multiple Git Accounts on One Device

How to Manage Multiple Git Accounts on One Device

Ever wondered having multiple git accounts in a same device, say if you’re working on a personal project and on another work project, you’ve configured two git accounts, but accidentally you push to via your personal account where you should have pushed with your work account. Yep, accidents do happen you can revert those changes, but learning from your mistakes should is a necessity.

So here I am, with this quick guide on configuring more than one git accounts. We’ll be using SSH keys and some basic gitconfig syntax.

Disclaimer: It is advised to read this blog in dark mode, to enhance the overall experience.

SSH vs HTTPS 🤔

We know in GitHub, we usually get two options to clone a repository(I’m ignoring github-cli here) .
And, Most Version Control Software support authentication with SSH or HTTPS: the preferred way is always using SSH where possible.
Git does not cache the user's credentials by default, so with HTTPS, you need to re-enter your PAT (Personal Access Token) each time you perform a clone, push, or pull.
On the other hand, SSH is safer and, when set up correctly (which will be once you follow this guide), you can completely forget about it!

Generating SSH keys

Before generating the keys, you need to know this equation,One SSH key pair = One git config.

We’ll be using rsa algorithm, for generating our SSH key pairs.

ssh-keygen -t rsa -b 4096 -C "your_personal_email@example.com" -f ~/.ssh/<personal_key>

-t : This flag is used for specifying the type of the key to be used. For example, rsa, ed25519, etc..

-C: For providing a comment, here, we’re using a meaningful comment for identifying keys by providing our emails

-f : To specify the filename of the key pair

Adding key in the SSH Agent

We will use ssh-agent to securely save your passphrase. Make sure the ssh-agent is running and add your key (the -K option is to store the passphrase in your key chain, macOS only).

eval "$(ssh-agent -s)" && ssh-add -K ~/.ssh/<personal_key>

Adding keys in the SSH config

We need to edit our config file inside the ~/.ssh directory, if not created then we need to create one using the touch ~/.ssh/config command.

Here, in the Host, which is a string, used when the ssh command connects to the remote host. For example, you can use github.com-work or github.com-personal in the Host.

Host <adding-host-name-1>
HostName github.com
User git
IdentityFile ~/.ssh/<ssh-key-for-account-1>

Host <adding-host-name-2>
HostName github.com
User git
IdentityFile ~/.ssh/<ssh-key-for-account-2>

Configuring public key in the VCS

For this setup, just copy your public key, which will be in this format of <created_key>.pub. You can do this using the following commands.

macOS

tr -d '\n' < ~/.ssh/<created_key>.pub | pbcopy

Linux

xclip -sel clip < ~/.ssh/<created_key>.pub

Windows

cat ~/.ssh/<created_key>.pub | clip

And simply just go to the SSH and GPG keys in the settings in GitHub.

Click on New SSH key and simply paste the public key under the Key and give it a meaningful title. Also, for the Key type leave it as Authentication Key only.

Structuring the workspace

Now comes the interesting parts of setting up the workspace, so we’ll be dividing our main folders under two sub directories personal and work in the /home/<user> directory.

In the /home/<User> directory will be our .gitconfig file, so the whole structure would look like in the format given below:

/home/<User>/
    |__.gitconfig
    |__work/
    |__personal/

Then, on top of this we’ll be override the gitconfig in the sub directories.

/home/<User>/
|__.gitconfig
|__work/
     |_.gitconfig.work
|__personal/
    |_.gitconfig.pers

Populating git configs

Here, an example of the gitconfig in the sub directories. First, we’ll have the .gitconfig.pers example:

# ~/personal/.gitconfig.pers

[user]
email = <personal-email>
name = <commit-author>

[github]
user = "<Personal-GitHub-username>"

[core]
sshCommand = "ssh -i ~/.ssh/<private-key-for-personal-account>"

Second, for the .gitconfig.work example:

# ~/work/.gitconfig.work

[user]
email = <work-email>
name = <commit-author>

[github]
user = "<Work-GitHub-username>"

[core]
sshCommand = "ssh -i ~/.ssh/<private-key-for-work-account>"

We can specify which SSH key to use by explicitly defining the SSH command with the -i option, which allows us to select an identity file or key. This approach provides greater control, especially in scenarios where you have multiple accounts on the same host (e.g., GitHub). Using an SSH config file to assign different keys would require creating custom prefixes (like first.github.com) and remembering to use them whenever cloning a repository for that account. This method can feel less flexible and somewhat inconvenient.

Now, comes the global .gitconfig example:

# ~/.gitconfig

[includeIf "gitdir:~/personal/"] # include for all .git projects under personnal/ 
    path = ~/personal/.gitconfig-pers

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig-work

[core]
excludesfile = ~/.gitignore      # valid everywhere

Since many settings, such as your name or editor preferences, are likely to be shared between configurations, you can define them here.

Keep in mind that Git has three levels of configuration that override each other: system, global, and local (project-specific). You can find more details about configuration options here.

To see a list of all config options

man git-config

To check your activated config options

git config --list

Repeat steps for other accounts

You can repeat the all these steps, for other git accounts, and configure those account safely and securely.

Testing our configs

Now, we can easily clone, push, add origin of multiple GitHub accounts.

But, first to test the ssh connection from Account-A, we can run the following command:

ssh -T git@github.com-personal

As you can see, in the above image response from github.

Cloning repository

In order to clone a repository, we have to modify the clone command a little bit for connecting to our configured Host.

git clone git@github.com-<personal or work>:deepanshu-rawat6/deepanshu-rawat6.git

We need to add -<personal or work> depending on our configured Host.

Add origin

git remote add origin git@<repository.domain.com>:<username>/<repo_name>.git

Final Words

So this was it for this blog everyone, I hope you’re able to configure multiple Git and GitHub accounts in your local machine.

If you liked this blog, a like would be appreciated, and do let me know areas of improvement. Also, if you want me to blog about some other services of AWS or DevOps, do let me know in the comments or you can reach me on Twitter.

See you on the next blog✨