Skip to main content
  1. Posts/

⚙️ Manage multiple git accounts

·344 words·2 mins·
opinions WIP
May Meow
Author
May Meow
MayMeow is a developer and cybersecurity enthusiast with a passion for cryptography, DevSecOps, and open-source contributions. They enjoy creating tools that strengthen digital security, blending creativity and technology to innovate in fields like PHP and .NET. Always exploring new frontiers in tech, MayMeow is dedicated to safeguarding the digital landscape through their work.
Table of Contents

This is an instruction only for Linux, I use this procedure also on Windows but this one is not described yet. I plan to add it as soon as I write it.

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

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

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> 

Then edit your ~/.ssh/config

Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/<created_key>

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

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.

Reply by Email

Related

My Selfhosting Workflow
·804 words·4 mins
linux WIP
Docker Volume on NFS?
·367 words·2 mins
docker linux WIP
⚙️ Sending Message to Mattermost With PHP and Github Actions
·1666 words·8 mins
php Github Actions