Have you ever had to use multiple git accounts on the same machine? If so, you know how annoying it is to have to change the git config every time you want to push to a different account. In this post, I will show you how to use multiple git accounts that will be used automatically depending on the directory you are in.
First, let's review the requirements to follow this tutorial
The first step is to create a new SSH key for each account you want to use. To do this, run the following command in your terminal:
You will be asked to enter a file name for the key. You can enter any name you want to, but I strongly recommend that you use a name that you can remember later. For example, if you are creating a key for your personal account, you can use id_personal as the file name.
After that, you will be asked to enter a passphrase. You can leave it blank if you want to, but I recommend that you enter a passphrase to make your key more secure.
Now that you have created your SSH key for each account, you need to add it to GitHub. To do this, run the following command in your terminal, this will copy the key to your clipboard.
Now go to your GitHub account settings and click on the "New SSH key" button. Enter a name for the key and paste the key you copied in the previous step.
Now that you have created your SSH keys and added them to your GitHub account, you need to create an SSH config file to tell your machine which key to use for each account. To do this, run the following command in your terminal:
If you already have an SSH config file, you can skip this step.
Now open the file you just created and add the following content to it:
Now you can test to see if everything is working properly by running the following command in your terminal:
Now we just need to create a git config file to tell git which account to use for each directory. To do this, run the following command for each account in your terminal:
Now open the file you just created and add the following content to it:
Now we need to tell git to use this config file for the directory we are in. To do this, run the following command in your terminal:
And edit the file to add the following content to it:
Now let's clone a private repository to see if everything is working. To do this, run the following command in your terminal:
Now go to the directory you just cloned and run the following command in your terminal:
If everything is working properly, you should see the email you used in the git config file.
In this post, I showed you how to use multiple git accounts on the same machine. I hope you enjoyed it and if you have any questions, feel free to reach out to me on Twitter or Email
$ ssh-keygen -t ed25519 -C "<account-email>"# Personal account
Host github.com-personal # Replace personal with the name you want to use
HostName github.com
User git
IdentityFile ~/.ssh/id_personal
# Work account
Host github.com-work # Replace work with the name you want to use
HostName github.com
User git
IdentityFile ~/.ssh/id_work[user]
name = <account-name>
email = <account-email>
[hub]
host = github.com# You can add a default account here
[include]
path = ./.config/git/config.personal # Replace personal with the name you used in the previous step
# You can change the account for a specific directory here
[includeIf "gitdir:**/<replace with the directory>/**/.git"]
path = ./.config/git/config.work # Replace work with the name you used in the previous stepEnter file in which to save the key (/Users/you/.ssh/id_ed25519): id_personalEnter passphrase (empty for no passphrase): [Type a passphrase]
Enter the same passphrase again: [Type passphrase again]# Replace id_personal with the name you used in the previous step
$ pbcopy < ~/.ssh/id_personal.pub$ touch ~/.ssh/config$ ssh -T git@github.com-personal # Replace personal with the name you used in the previous step
$ ssh -T git@github.com-work # Replace work with the name you used in the previous step$ touch ~/.config/git/config.personal # Replace personal with the name you want to use$ git config --global -e# Replace personal with the name you used in the previous step
$ git clone git@github.com-personal:<account-name>/<repository-name>.git$ git config user.name