Mostly, we do own more than one git accounts, at least two accounts, one work account and one personal. I have been using 3 git accounts regularly, one GitHub, one bitbucket and one gitlab account. I had configured them as and when I have created. Recently, I got a new laptop and I needed to configure them again so I thought to have a one blog post (this is my first blog post by the way 🙂).
Following are the sample accounts (no the actual one) which I will be using in this post to demonstrate setting up multiple git accounts on local machine. Their respective configuration details are as follow:
Provider | Username | SSH Key File | |
Bitbucket | Bitbucket User | bitbucket@work.com | work_key |
GitHub | GitHub User | github@personal.com | github_key |
GitLab | GitLab User | gitlab@perosnal.com | gitlab_key |
Following is the folder structure on my local machine to manage work related and personal git repositories separate. All the work related repositories are stored under the ~/work/git
and all the personal git repositories are ~personal/github/repo
and ~personal/gitlab/repo
for the github and gitlab accounts respectively.
~/
|__.gitconfig
|__work
| |__bitbucket
| |__.gitconfig-work
| |__bitbucket-test-repository
|
|__personal
| |__github
| | |__.gitconfig-github
| | |__github-test-repository
| |
| |__gitlab
| |__.gitconfig-gitlab
| |__gitlab-test-repository
Generate SSH Keys
You need to generate different ssh keys for different accounts. You can also use same/existing key but I preferred to generate new keys using the following commands. Please note that I generate these keys under the ~/.ssh
but you can store them anywhere.
ssh-keygen -t rsa -b 4096 -C "bitbucket@work.com" -f ~/.ssh/work_key
ssh-keygen -t rsa -b 4096 -C "github@personal.com" -f ~/.ssh/github_key
ssh-keygen -t rsa -b 4096 -C "gitlab@perosnal.com" -f ~/.ssh/gitlab_key
Add public keys to respective server
There should be a 3 pair of keys, one public key (file with extension .pub
) and one private key for each account. Now you need to add your public key content to respective account i.e. content of work_key.pub should be added to Bitbucket account, content of github_key.pub should be added to GitHub account and the content of gitlab_key.pub should be added to GitLab account. For more detail to setup ssh keys for specific provider, please refer GitHub, GitLab or BitBucket.
cat ~/.ssh/work_key.pub
cat ~/.ssh/github_key.pub
cat ~/.ssh/gitlab_key.pub
Create git configs for each account
Now, it’s time to create git config files for each account. Here, we configure user’s email address, name and the path of the ssh key to be used for that particular git account. Following are the git config files for all 3 accounts. These configuration files will be used when you perform any git action for that account. How git determines which git config file to use is covered in next section.
# ~/work/bitbucket/.gitconfig-work
[user]
email = bitbucket@work.com
name = Bitbucket User
[core]
sshCommand = "ssh -i ~/.ssh/work_key"
# ~/personal/github/.gitconfig-github
[user]
email = github@personal.com
name = GitHub User
[core]
sshCommand = "ssh -i ~/.ssh/github_key"
# ~/personal/gitlab/.gitconfig-gitlab
[user]
email = gitlab@perosnal.com
name = GitLab User
[core]
sshCommand = "ssh -i ~/.ssh/gitlab_key"
Git Configuration for each Account
Now we have to instruct git client about which git configuration file it has to use when we perform any git action. Here, we have give instructions to decide this based on the location of the git repository i.e. where that git repository is located. It works as follow:
- If the git repository located inside the
~/work/bitbucket/
then it will use the configurations from the~/work/.gitconfig-work
git config file. - If the git repository located inside the
~/personal/github/
then it will use the configurations from the~/personal/github/.gitconfig-github
git config file. - If the git repository located inside the
~/personal/gitlab/
then it will use the configurations from the~/personal/gitlab/.gitconfig-gitlab
git config file.
Following is the git config file.
# ~/.gitconfig
# include for all the work related Bitbucket projects under ~/work/bitbucket/
[includeIf "gitdir:~/work/bitbucket/"]
path = ~/work/.gitconfig-work
# include for all the personal GitHub projects under ~/personal/github/
[includeIf "gitdir:~/personal/github/"]
path = ~/personal/github/.gitconfig-github
# include for all the personal GitLab projects under ~/personal/gitlab/
[includeIf "gitdir:~/personal/gitlab/"]
path = ~/personal/gitlab/.gitconfig-gitlab
Clone Repositories
Let’s clone all the repository to local machine using the following commands. If everything is setup correctly then you should be able to clone them without any issues. So, cloning all the repository successfully indicates that we have configured different ssh keys for different accounts correctly.
git -C ~/work/bitbucket/ clone git@bitbucket.org:atulprajapati/bitbucket-test-repository.git
git -C ~/personal/github/ clone git@github.com:atulprajapati/github-test-repository.git
git -C ~/personal/gitlab/ clone git@gitlab.com:atulprajapati/gitlab-test-repository.git
Check other configurations
Now we need confirm that other settings i.e. user’s name and email address are configured correctly. Try running git config –get user.name and git config –get user.email from each git repository you have cloned.
- For the work git repository (in my case, it is “~/work/bitbucket/bitbucket-test-repository”),
git config --get user.name
should returnBitbucket User
andgit config --get user.email
should returnbitbucket@work.com
. - For the personal github repository (in my case, it is “~/personal/github/github-test-repository”),
git config --get user.name
should returnGitHub User
andgit config --get user.email
should returngithub@personal.com
. - For the personal gitlab repository (in my case, it is “~/personal/gitlab/gitlab-test-repository”),
git config --get user.name
should returnGitLab User
andgit config --get user.email
should returnbitbucket@work.com
.
Test with Sample Commits
Let’s confirm that appropriate username and email address is used when we commit any change to these repositories. I have updated one file in each repository and committed that change. To confirm that appropriate username and email addresses are used, I have used git log origin/master..HEAD
command and looked at logs to confirm that the setup is working as expected an all good.
Please look at the below git logs for each commit made against different repository. Please refer the Author:
to confirm that commit uses proper username and email address.
# For Work (i.e. Bitbucket) Account
atul@takshmind:~/work/bitbucket/bitbucket-test-repository$ git log origin/master..HEAD
commit f8956b5884c29405b08d508a8774c4a9ab1b8a54 (HEAD -> master)
Author: Bitbucket User <bitbucket@work.com>
Date: Sun Feb 19 06:46:05 2023 +0000
Test commit to check git configuration
# For Personal (i.e. GitHub) Account
atul@takshmind:~/personal/github/github-test-repository$ git log origin/master..HEAD
commit 09ba9ed67c391211f7c873046d10330be15b6bc5 (HEAD -> master)
Author: GitHub User <github@personal.com>
Date: Sun Feb 19 06:43:43 2023 +0000
Test commit to check git configuration
# For Personal (i.e. GitLab) Account
atul@takshmind:~/personal/gitlab/gitlab-test-repository$ git log origin/master..HEAD
commit 6494198287a8b6fb4784f1e2bbd2ae632e3ccea9 (HEAD -> master)
Author: GitLab User <gitlab@perosnal.com>
Date: Sun Feb 19 06:38:57 2023 +0000
Test commit to check git configuration
This is how I manage multiple git accounts locally to work with them efficiently. This setup requires some efforts initially but I believe, it’s worth putting these efforts.