DEV Community

Martin Licht
Martin Licht

Posted on

How to use Git with private repositories on GitHub

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

  1. The following steps should work on any Linux shell and any other Unix-like shell, such as Cygwin or Git Bash.

  2. First, we create a 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, change the previous line to:

    ssh-keygen -t ed25519 -f ~/.ssh/githubkey
    

    The ssh-keygen command 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.

  3. We can inspect the content of the public key file as follows:

    cat ~/.ssh/githubkey.pub
    
  4. Now go to the list of SSH keys for your GitHub account, which you find at:

    GitHub SSH Keys Settings

    Click on the button New SSH key. Enter some name for the new key and copy the content of githubkey.pub into the Key text field.

    You can also find instructions for this step in the official documentation.

  5. 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`
    
  6. In a modern Linux environment, the SSH agent should look for SSH keys inside the .ssh directory. In any case, if needed, we manually add the SSH key:

    ssh-add ~/.ssh/githubkey
    
  7. Let us test the SSH connection by trying shell access on Github:

    ssh -T git@github.com
    

    GitHub will confirm authentication with the message:

    Hi username! You've successfully authenticated, but GitHub does not provide shell access.
    
  8. If you have a repository on GitHub that you want to clone, you use:

    git clone git@github.com:username/your-repository.git
    

    Note that your username for SSH login is git, which is perhaps somewhat counterintuitive.

  9. If you already have a local Git repository, then you proceed as follows. First, we navigate to your 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
    

    We replace nickname_of_remote_repo with the appropriate name. A common choice is origin but you might have chosen something else. Now you are all set!


Special instructions for Git Bash

  1. 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.

  2. 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
    
  3. It would be much more convenient if that command were executed on every login. Git Bash uses .profile for any commands that executed on login. Let us first make sure it exists:

    cd
    touch .profile
    

    Open that file in any editor and add

    eval 'ssh-agent -s' && ssh-add ~/.ssh/githubkey
    
  4. Now the ssh agent will start whenever you open a Git Bash shell and the requested keys will be added.

  5. 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/null
    

    This modification redirects any output except errors.


Top comments (0)