Git config for multiple users (office/personal)
Paulo de Tasso Oliveira de Lacerda
Posted on June 19, 2023
Create the folders:
~/Development
/github # personal projects
/findme# work projects
Create two files:
~/.gitconfig-github
# .gitconfig-github
[user]
email = paulodetasso@proton.me
~/.gitconfig-findme
# .gitconfig-findme
[user]
email = paulo@findme.id
in ~/.gitconfig we will paste this code:
[user]
name = Paulo Lacerda
[init]
defaultBranch = main
[includeIf "gitdir:~/Development/github/"]
path = .gitconfig-github
[includeIf "gitdir:~/Development/findme/"]
path = .gitconfig-findme
SSH
Run:
mkdir ~/.ssh
cd ~/.ssh
Run this code in terminal:
ssh-keygen -t rsa -C "paulodetasso@proton.me" -f "id_rsa_personal"
ssh-keygen -t rsa -C "paulo@findme.id" -f "id_rsa_work"
And then:
ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_work
After that, we need to configure SSH to understand when to use each key. To do that, we'll create a config file inside the .ssh folder:
cd ~/.ssh
touch config
code config # you can use vi, vim, nano, or your preferred text editor
Inside the config file, you can edit it as follows:
# Personal account as default
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
# Work account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
In the example above, it uses "github" as the host, but if you're using GitLab, you can change it in both the Host and HostName lines. Additionally, you can have different configurations for different remote repositories within the same file.
So, whenever you clone a repository, if it belongs to your work account, you just need to edit the URL to match the structure above:
git clone git@github.com-work:your_user/repo_name.git
Posted on June 19, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.