Git | Using multiple accounts with Github

Inhaltsverzeichnis

Create a New SSH Key

We need to generate a unique SSH key for our second GitHub account.

$ ssh-keygen -t rsa -C <your email address>

Be careful: do not over-write your existing key for your personal account. Instead, when prompted, save the file as id_rsa_<account name>.

I saved the file to ~/.ssh/id_rsa_placeholder

Attach the new Key to your Github Account

Next, login to your second GitHub account, browse to “Account Overview,” and attach the new key, within the “SSH Public Keys” section.

Copy and paste the content of the generated ssh file (with extention .pub):

~/.ssh/id_rsa_placeholder.pub

Note: Be sure to give this key a descriptive title, so that you can remember the source of the key.

Configure you shell environment

Start the ssh-agent, so that we can remember ssh-keys.

$ eval $(ssh-agent)
Agent pid 5099

Note: If you want to automatically start the agent on login, please look at the profile script at the end of this article

Now, add the key to the ssh-agent

$ ssh-add $HOME/.ssh/id_rsa_placeholder
Identity added: /home/ralphg/.ssh/id_rsa_placeholder (/home/placeholder/.ssh/id_rsa_placeholder)

Verify, that the key is loaded

$ ssh-add -l
2048 SHA256:ZOvzhxxxMVxxxOsMxxxm2YxxxHpV4/eAiFWyKJVRl/xxxxx /home/placeholder/.ssh/id_rsa_placeholder (RSA)

Now, configure your github repository

$ git remote -v 
origin https://github.com/placeholder/Awesome-Github.git (fetch)
origin https://github.com/placeholder/Awesome-Github.git (push)

Configure repository to use this ssh-key

$ git remote remove origin
$ git remote add origin git@github.com:placeholder/Awesome-Github.git
$ git remote -v
origin  git@github.com:placeholder/Awesome-Github.git (fetch)
origin  git@github.com:placeholder/Awesome-Github.git (push)
$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
$ git push --set-upstream origin master
The authenticity of host 'github.com (140.82.118.4)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,140.82.118.4' (RSA) to the list of known hosts.
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 5.55 KiB | 567.00 KiB/s, done.
Total 5 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:placeholder/Awesome-Github.git<
    1679693..3bbeede  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.<

Configure your repository to use the created ssh-key

$ git config core.sshCommand "ssh -i ~/.ssh/id_rsa_placeholder"

Appendix

Shell profile script for autostart ssh-agent

SSH_ENV="$HOME/.ssh/environment"

function start_agent {
    echo "Initialising new SSH agent..."
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

Script: create_ssh_keys

#!/bin/bash

GITHUB_ACCOUNT="$1"
echo ssh-keygen -t rsa -C "${GITHUB_ACCOUNT}@via-internet.de" -f id_rsa_${GITHUB_ACCOUNT}

Script: update_ssh_keys_to_HOME

#!/bin/bash
FLDR_DST=$HOME/.ssh

for _FILE in id*
do
    echo "copy $_FILE"
    FILE_DST="${FLDR_DST}/$(basename $_FILE)"

    chmod 600       $FILE_DST
    cp  $_FILE      $FLDR_DST
    chmod 400       $FILE_DST
done