git

Git config for multiple users (office/personal)

tassolacerda

Paulo de Tasso Oliveira de Lacerda

Posted on June 19, 2023

Git config for multiple users (office/personal)

Create the folders:

~/Development
   /github # personal projects
   /findme# work projects
Enter fullscreen mode Exit fullscreen mode

Create two files:

~/.gitconfig-github

# .gitconfig-github

[user]
  email = paulodetasso@proton.me
Enter fullscreen mode Exit fullscreen mode

~/.gitconfig-findme

# .gitconfig-findme

[user]
  email = paulo@findme.id
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

SSH

Run:

mkdir ~/.ssh
cd ~/.ssh
Enter fullscreen mode Exit fullscreen mode

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" 
Enter fullscreen mode Exit fullscreen mode

And then:

ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_work
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
tassolacerda
Paulo de Tasso Oliveira de Lacerda

Posted on June 19, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related