Generate An SSH Keypair

Click here to follow a quick start SSH key generation guide.

I’m going to use the id_my_eliptic_curve keypair generated in that guide, your chosen keypair name may vary.

Upload Your Public Key To Github

You need to let git hub know about your SSH keypair. You do this by uploading your public key to your github account.

Under settings>SSH and GPG keys select New SSH Key.

Give your key a title, and paste the contents of your public key file, in my case the contents of my id_my_eliptic_curve.pub file.

Click Add SSH key, and you will be brought back to you SSH and GPG keys settings page.

Make An Entry In Your ~/.ssh/config File

We need to let Ubuntu know about your new keypair identity files and how you want to use them.

If your .ssh directory does not have a config file, create one. Then add the following.

# github account
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_my_eliptic_curve
  • # github account : This is just a comment line for your own reference.
  • Host github.com : This is an alias for the host you want to use your key with.
  • HostName github.com : The actual host address you want to connect with.
  • PreferredAuthentications publickey: Just to set that you prefer to use publickey authentication.
  • IdentityFile ~/.ssh/id_my_eliptic_curve : The location of the key you want to use to connect to github.

Cloning A Repository

You need to retrieve the SSH URL for the repository you want to check out.
Make sure you select the SSH tab.

Copy that URL and use the following command to check out your repository.

git clone [email protected]:{yourgitusername}/{yourreponame}.git

You should see output like this. I have cloned my repository in to a local directory ~/myrepos which is where I keep all my checked out projects.

[email protected]:~/myrepos$ git clone [email protected]:{yourgitusername}/{yourreponame}.git
Cloning into 'yourreponame'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 13 (delta 1), reused 5 (delta 0), pack-reused 0
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (1/1), done.

Using More Than One Github Account

Well, if you have come this far this will be very easy to setup.

Add New Entry To Your ~/.ssh/config File

Here is the modified ~/.ssh/config file. The new entry still describes a connection to github but we are using a different SSH key.

# github account
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_my_eliptic_curve

# my other github account
Host banana.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_my_banana_curve

Your clone command will use the alias banana.github.com instead of github.com. The following command will clone a repository from your second github account.

git clone [email protected]:{yourothergitusername}/{yourreponame}.git

If you have registered the same SSH key with both github accounts you could still use the same original clone command with host github.com.


I do think keeping different SSH keypairs for different accounts and adding different host entries in your config file is better practice.

Switching config user.email And user.name

If you are going to be connecting to different github accounts you might want to use a different name and email while committing to each different project. You may want to have a work email associated with one project, and a personal email with another.

Open your ~/.gitconfig file for editing and an alias section, add as many aliases as you need for as many accounts as you want. I have two here, setnew sets your new user details for a project, and setorig reverts them.

[user]
        email = [email protected]
        name = your original name
[credential]
        helper = store
[alias]
        setorig = "!f() { git config user.email '[email protected]' && git config user.name 'your original name'; }; f"
        setnew  = "!f() { git config user.email '[email protected]' && git config user.name 'your new name'; }; f"

You can now switch your email and user name from within a checked out project. These alias commands add entries to your projects .git/config file
which overrides the global gitconfig file. This is a project by project approach.

Here is what it looks like if you run the alias and check your project config.

[email protected]:~/myrepos/project1$ git setnew
[email protected]:~/myrepos/project1$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = [email protected]:{yourgitusername}/project1.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main
[user]
        email = [email protected]
        name = your original name

You can always switch back using,

[email protected]:~/myrepos/project1$ git setnew
%d bloggers like this: