-
The following steps should work on any Linux shell and any other Unix-like shell,
such as Cygwin or Git Bash.
-
First things first, we create new pair of private and public keys to be used for Github.
ssh-keygen -t rsa -b 4096 -f ~/.ssh/githubkey
This line will generate two files githubkey
and githubkey.pub
within the .ssh
subdirectory of your home directory.
The first file is the private key and the second file is the public key.
Instead of an RSA key pair, you might opt to use an ed25519 key pair instead.
This new type of key pair is faster and safer.
Some older servers may not support it, but Github does.
If that is your choice, you change the previous line to:
ssh-keygen -t ed25519 -f ~/.ssh/githubkey
In either case, the command ssh-keygen
will ask you for an optional passphrase.
You may just leave them blank.
Otherwise, any SSH connection will need you to enter the passphrase as an additional security feature.
-
You can inspect the content of the public key file:
cat ~/.ssh/githubkey.pub
-
Now you go the list of SSH keys for your Github account.
You find them at:
https://github.com/settings/keys.
Click on the button
New SSH key
.
Now you enter an name for the new key
and copy the content of githubkey.pub
into the Key
textfield.
You find instructions for this step in the
official documentation as well.
-
We need an SSH agent running in the background.
On Linux, the ssh agent should already be running in the background.
Regardless, if the ssh agent is not running yet,
then you simply execute
eval `ssh-agent -s`
to get the agent started in the background of your current shell.
-
In a modern Linux environment,
the SSH agent already looks for SSH keys inside the
.ssh
directory on a per-use basis.
In other environments, we need to manually add the SSH key:
ssh-add ~/.ssh/githubkey
-
Let us test the SSH connection by trying shell access on Github:
ssh -T git@github.com
Github will confirm that we can authenticate.
You should receive the message
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
-
Either you already have a repository on Github that you want to clone,
or you already have a local repo that you want to connect.
Let us address the former case first and then the latter case.
You can clone an existing repo from Github into your current working directory via
git clone git@github.com:username/your-repository.git
Note that your username for SSH login purposes is simply git
,
which is perhaps somewhat counterintuitively.
Now you are all set!
-
Now we address the case where you already have a local Git repository.
You must then indicate to Git how and where to reach the remote repository on Github.
First, make sure you are in the directory of your local repo:
cd /path/to/your/repo
You see the known remote repositories and their urls via
git remote -v
If you already see a line like
nickname_of_remote_repo git@github.com:username/your-repository.git
then you are good to go. Otherwise, you can either add the new remote repository via
git remote add nickname_of_remote_repo git@github.com:username/your-repository.git
or you change the address of an existing one via
git remote set-url nickname_of_remote_repo git@github.com:username/your-repository.git
Please pick the right name instead of nickname_of_remote_repo
.
The most common name is origin
but you might have chosen something else.
Now you are all set!