I am trying since more than 3 hours to setting up multiple account for github and litteraly tired. I have tried out almost all possible way describe here, github and article
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 workemail@domain.com homeemail@domain.com"
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 git@gitlab.com:<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.
So according to @VonC's answer here what I have done.
ssh -T ac2.github.com
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
I am using this script, that switch indentity and other all neccessary like git setting
https://gist.github.com/milosjanda/a86ebc039293f22fabe723024ec42b46
if [[ -f ~/.ssh/.work ]]; then
echo "swith to HOME"
# mv ~/.ssh/id_rsa ~/.ssh/work; mv ~/.ssh/home ~/.ssh/id_rsa
rm ~/.ssh 2> /dev/null
ln -s ~/.ssh-work/home ~/.ssh
git config --global user.email "my.name@gmail.com"
else
echo "swith to WORK"
# mv ~/.ssh/id_rsa ~/.ssh/home; mv ~/.ssh/work ~/.ssh/id_rsa
rm ~/.ssh 2> /dev/null
ln -s ~/.ssh-work/work ~/.ssh
git config --global user.email "my.name@company.eu"
fi
# Delete all identities from ssh-agent
ssh-add -D
# Add new identity to ssh-agent
ssh-add
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.