Skip to content

Instantly share code, notes, and snippets.

@jbsummoner
Last active June 20, 2021 05:07
Show Gist options
  • Save jbsummoner/ee2493213e595422e076aa1ae636d6f4 to your computer and use it in GitHub Desktop.
Save jbsummoner/ee2493213e595422e076aa1ae636d6f4 to your computer and use it in GitHub Desktop.
managing mulitple git accounts on one machine

Working parts

  1. private key for each account
  2. gitconfig files for each git account
  3. update .gitconfig to condtionally switch between users
  4. update ssh config file to use keys, and unique hostname for each user
  5. update usage -> git remote addresses to match users ssh config

.gitconfig

# ~/.gitconfig

#default account
[include]
	path = ~/Documents/gitconfigs/git-personal.conf

#second account
[includeIf "gitdir:~/path/to/two/"]
	path = ~/Documents/gitconfigs/git-user-2.conf
[filter "lfs"]
	clean = git-lfs clean -- %f
	smudge = git-lfs smudge -- %f
	process = git-lfs filter-process
	required = true

#third account
[includeIf "gitdir:/path/to/three/"]
	path = ~/Documents/gitconfigs/git-user-3.conf
[filter "lfs"]
	clean = git-lfs clean -- %f
	smudge = git-lfs smudge -- %f
	process = git-lfs filter-process
	required = true

Git config files

# git-work.conf 
[user]
	name = enter_name
	email = enter_email

.ssh/config

# ~/.ssh/config

# default
Host github.com
        User git
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile /path/to/account/key

# another account 2
# syntax: github.com-[user]
Host github.com-user-2
        HostName github.com
        User git
        PreferredAuthentications publickey
        IdentityFile /path/to/second/account/key

# “github.com-user-2” is a notation used to differentiate the multiple Git accounts. You can also use 
# “work_user1.github.com” notation as well. Make sure you’re consistent with what hostname notation you use. This is relevant 
# when you clone a repository or when you set the remote origin for a local repository

Addional notes

might have to set approiate account key to ssh agent

# delete keys
ssh-add -D

# add appropiate ssh key you need at the time
ssh-add ~/.ssh/id_rsa_user

Usage

git clone git@github.com-user-2:user-2/repo_name.git
git remote set-url origin git@github.com-user-2:user-2/repo_name.git
References

https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment