Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

How to use multiple ssh for github on Mac

Generate ssh keys

Use these commands to generate ssh key for every user we need

ssh-keygen -t rsa -C "user1@email.com" -f "user1"
ssh-keygen -t rsa -C "user2@email.com" -f "user2"
ssh-keygen -t rsa -C "user3@email.com" -f "user3"
...

These commands generate a pair of key files in ~/.ssh

user1 user1.pub
user2 user2.pub
user3 user3.pub

Add ssh keys to agent

ssh-add -k ~/.ssh/user1
ssh-add -k ~/.ssh/user2
ssh-add -k ~/.ssh/user3

note: we need to run ssh-add everytime we restart the laptop, or we can add this command automatically in our zsh on bootstrap.

Add keys to every github profile

copy every pub key and past it in the ssh section in every github profile

for copy the content without opening the file use this command

pbcopy < ~/.ssh/user1.pub

Check global profile, this will be used by default

git config --global -e
[user]
        name = user1
        email = user1@email.com

Test connection

ssh -T git@github.com
Hi user1! You've successfully authenticated, but GitHub does not provide shell access.

by default every repo will use the global one.

clone the repo with the correct ssh key

git clone git@provider.com:user1/projectName.git --config core.sshCommand="ssh -i ~/.ssh/user1"

define local github user

To use a different user we need modify the repo .git/config file

in the repo folder run git config --local -e to access and edit the file

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = git@github.com:user2/reponame.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main

we need to add the user block and the sshCommand in the core block as following

[user]
        name=user2
        email=user2@email.com
[core]
        sshCommand = ssh -i ~/.ssh/user2 -F /dev/null
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = git@github.com:dcomedevelopers/learning-react-query.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main

from now on this repo will use the defined user automatically.

SSH credentials issue when not authorized

Get the list of existing ssh added in the list ssh-add -l

3072 SHA256:xxxxxx user1@gmail.com (RSA)
3072 SHA256:/xxxx user2@gmail.com (RSA)
3072 SHA256:/xxxx user3@gmail.com (RSA)

when a local user is not defined in the local config file in the repo, it can happen that we get an auth problem because git won't be able to resolve the correct ssh to use and will try to use the first one.

so there are 2 solution, adding the local user block in the git config file as explained above, or removing the other keys. This command just remove the ssh key from the list, them can be added later with the ssh-add command

This command will remove the extra 2 ssh keys in order to access a repo as user3

ssh-add -d ~/.ssh/user1
ssh-add -d ~/.ssh/user2

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors