GitHub: Multiple account setup

So according to @VonC's answer here what I have done.

  1. I have generate ssh key for another account and added with id_rsa_ac2 (where ac2 is for second account)
  2. Than just cross checked either it works with ssh -T ac2.github.com
  3. Created config file (without extention) in /c/Users/yourname/.ssh/ directory

Here is the code what I used for config file

#Account one
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile /c/Users/yourname/.ssh/id_rsa
    User git

#Account two
Host ac2.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile /c/Users/yourname/.ssh/id_rsa_ac2
    User git

So now once you done this you can start to use both account as you need.

for main account I added remote as origin with git remote add origin git@github/youraccount/rep.git Than to push use git push origin master this will upload to your first account.

To add remote for second (ac2) account used git remote add ac2 ac2.github/yoursecondaccount/rep.git Than to push use git push ac2 master this will upload to the second (ac2) account.

To check if it has added remote use git remote -v and incase if you want to remove anyone than use git remote rm origin where origin is your added remote.

Hope this information will helps to other who is having the same issue.

Thanks again to @VonC


For your config to be taken into account, you need to use its Host name in your remote address:

git remote add origin ac2.github.com:myaccount/my

If you have defined a HOME environment variable (which isn't defined by default on Windows, but is defined if you are using the msysgit git-cmd.bat) to a directory under which you have your .ssh directory, with its id_rsa_ac2 private key, and id_rsa_ac2.pub public key, then it will work.


Here is a script to Automate the addition of two GitLab Accounts to your setup.

setup-gitlab.sh

#!/bin/bash

# VERIFIED FOR FEDORA 27 MATE (Likely to work in others distros)
# Multi Account SSH for GitLab/OpenSSH Setup.
ROOT=root
if (( whoami == $ROOT ))
    then 
    echo "Run as standard user"
elif [[ -z $1 || -z $2 ]]
    then
    echo "command usage: setup-gitlab.bash [email protected] [email protected]"
elif [[ ! $1 =~ .*@.*\..* ]]
    echo "Work email is not in the correct format. Must match regex .*@.*\..*"
elif [[ ! $2 =~ .*@.*\..* ]]
    echo "Home email is not in the correct format. Must match regex .*@.*\..*"
else
    HOMEEMAIL=$1
    WORKEMAIL=$2
    USRNAME=`whomai`

# /home/<username>/.ssh/
# ├── config
# ├── home-gitlab-key
# ├── home-gitlab-key.pub
# ├── known_hosts
# ├── work-gitlab-key
# └── work-gitlab-key.pub

#Executed to match the above directory.
    ssh-keygen -t rsa -C "$WORKEMAIL" -b 4096 -f work-gitlab -N ""
    ssh-keygen -t rsa -C "$HOMEEMAIL" -b 4096 -f home-gitlab -N ""

# Agent Configuration Setup (.ssh/config)
    cat >> ~/.ssh/config <<EOF
Host gitlab-work
  HostName gitlab.com
  User git
  IdentityFile /home/$USRNAME/.ssh/work-gitlab-key

Host gitlab-home
  HostName gitlab.com
  User git
  IdentityFile /home/$USRNAME/.ssh/home-gitlab-key
EOF

# Agent Setup (potentially optional???)
    cat >> ~/.bashrc <<'EOF'
eval "$(ssh-agent -s)"
for i in `ls ~/.ssh/*.pub` ; do ssh-add ${i::-4} ; done
EOF

    . .bashrc

fi

After running the script you will need to copy the contents of the two public keys created to each GitLab account respectively.

Another note, when using git clone [email protected]:<account>/<project>.git your should replace gitlab.com as follows.

git clone git@gitlab-home:<account>/<project>.git

and

git clone git@gitlab-work:<account>/<project>.git

respectively.

Tags:

Github