Manage multiple git accounts

maymeow

Meow

Posted on October 24, 2022

Manage multiple git accounts

Hi! Do you have multiple git accounts or you just using one? I have multiple and everytime I reinstall my system I trying to find how I
configured it before... (i finally wrote this together here).

Im using multiple git config file rather than multiple host names (i don't like to have somethink like git@github-work or git@github-personal).

How it's work?

First of all you need to create different folders for you personal and work projects (or more folders if you have more accounts). Into each folder
create new .gitconfig file which will looks like this one:

# ~/Work/.gitconfig.work
[user]
email = your@email
name = Your Name

[github]
user = "YourGithubName"

[core]
sshCommand = "ssh -i ~/.ssh/work.pub" # public key for your work profile
Enter fullscreen mode Exit fullscreen mode

You will also need more SSH keys. I using 1Password so I copy only my public keys to the my .ssh folder.

SSH with 1Password

If you want to use 1Passowrd you need to configure ssh. Add following to your ~/.ssh/config file

Host *
    IdentityAgent ~/.1password/agent.sock
Enter fullscreen mode Exit fullscreen mode

SSH With SSH-AGENT

If you want to use more traditional way you will need to configure SSH-AGENT

eval "$(ssh-agent -s)" && \
ssh-add -K ~/.ssh/<personal_key> 
Enter fullscreen mode Exit fullscreen mode

Then edit your ~/.ssh/config

Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/<created_key>
Enter fullscreen mode Exit fullscreen mode

OK now you have create your keys and gitconfig files for you profiles. Now you need to tell git to use proper config based on git repository location. Edit ~/gitconfig which will need to looks similar to this:

# ~/.gitconfig

[includeIf "gitdir:~/May/"] # include for all .git projects under May/ 
path = ~/May/.gitconfig.may

[includeIf "gitdir:~/Work/"] # include for all .git projects under Work/
path = ~/Work/.gitconfig.work

[core]
excludesfile = ~/.gitignore      # valid everywhere
Enter fullscreen mode Exit fullscreen mode

You list all your configs with git config --list command.

That's all

Good! You can now clone repositories from multiple accounts based on in which folder you clonning it.

💖 💪 🙅 🚩
maymeow
Meow

Posted on October 24, 2022

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

Sign up to receive the latest update from our blog.

Related