This guide will show you how to access private repositories on Github. This is not as straight-forward as it used to be since Github has made SSH mandatory for accessing private repositories a few years ago. So some low-level set up is required to make this work. I found the official documentation by Github to be lacking a few details, which is why I have written this guide.
Public repositories can be accessed via SSH or the more traditional HTTPS, so this does not require any special setup.
Table of Contents
Creating the SSH key pair and setting the repo URL
The following steps should work on any Linux shell and any other Unix-like shell, such as Cygwin or Git Bash.
-
First, we create a new pair of private and public keys to be used for GitHub:
ssh-keygen -t rsa -b 4096 -f ~/.ssh/githubkeyThis line will generate two files
githubkeyandgithubkey.pubwithin the.sshsubdirectory 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, change the previous line to:
ssh-keygen -t ed25519 -f ~/.ssh/githubkeyThe
ssh-keygencommand will ask you for an optional passphrase. You may just leave them blank, or enter one for additional security. Otherwise, any SSH connection will need you to enter the passphrase as an additional security feature. -
We can inspect the content of the public key file as follows:
cat ~/.ssh/githubkey.pub -
Now go to the list of SSH keys for your GitHub account, which you find at:
Click on the button New SSH key. Enter some name for the new key and copy the content of
githubkey.pubinto the Key text field.You can also find instructions for this step in the official documentation.
-
We need an SSH agent running in the background. On Linux, the SSH agent should already be running. If it is not, we start it with:
eval `ssh-agent -s` -
In a modern Linux environment, the SSH agent should look for SSH keys inside the
.sshdirectory. In any case, if needed, we 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.comGitHub will confirm authentication with the message:
Hi username! You've successfully authenticated, but GitHub does not provide shell access. -
If you have a repository on GitHub that you want to clone, you use:
git clone git@github.com:username/your-repository.gitNote that your username for SSH login is
git, which is perhaps somewhat counterintuitive. -
If you already have a local Git repository, then you proceed as follows. First, we navigate to your repo:
cd /path/to/your/repoYou see the known remote repositories and their urls via
git remote -vIf you already see a line like
nickname_of_remote_repo git@github.com:username/your-repository.gitthen 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.gitor you change the address of an existing one via
git remote set-url nickname_of_remote_repo git@github.com:username/your-repository.gitWe replace
nickname_of_remote_repowith the appropriate name. A common choice isoriginbut you might have chosen something else. Now you are all set!
Special instructions for Git Bash
-
Git Bash is a lightweight Bash emulation for Windows. It provides a limited Unix-like command line on Windows with specific focus on Git functionality.
Git Bash is part of the Git for Windows package. While this package also includes a graphical user interface, I am going to focus on the bash emulation here.
-
The above instructions apply in the same manner when you use Git Bash. However, the SSH agent is generally not started in the background by default. We start it manually:
eval 'ssh-agent -s' && ssh-add ~/.ssh/githubkey -
It would be much more convenient if that command were executed on every login. Git Bash uses
.profilefor any commands that executed on login. Let us first make sure it exists:
cd touch .profileOpen that file in any editor and add
eval 'ssh-agent -s' && ssh-add ~/.ssh/githubkey Now the ssh agent will start whenever you open a Git Bash shell and the requested keys will be added.
-
Depending on personal preferences, you may want to apply some tweaks. Some people prefer to login script to be silent:
eval 'ssh-agent -s' > /dev/null && ssh-add ~/.ssh/githubkey > /dev/nullThis modification redirects any output except errors.
Top comments (0)