Skip to content

Tutorial for adding ssh keys on github and command line

Notifications You must be signed in to change notification settings

morganrivers/how_to_ssh_key

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 

Repository files navigation

Adding SSH Keys on GitHub

This tutorial explains how to add SSH keys on GitHub and the command line with linux and bash, usually for pushing / pulling from a private repo. This is also particularly useful if you've encountered this error:

remote: Support for password authentication was removed on August 13, 2021.
Please see this link for information on currently recommended modes of authentication.

Prerequisites

Before we begin, you will need the following:

  1. Your GitHub organization or username: git_username_or_org (replace with your username or organization name)
  2. Your Git email: [email protected] (replace with your email)
  3. A short name for your SSH key file (to identify it later): key_name_for_file (replace with your preferred name relating to the repository)
  4. Your repository name: repo_name (replace with your repository name)
  5. Finally, ensure you've gone through the git config commands already. If you're not sure, you can run:
git config --global user.name

and

git config --global user.email

These commands will print out the global Git username and email if they have been set. If they haven't been set, they will not return any output.

If they do not return output, then run:

git config --global user.name git_username_or_org
git config --global user.email [email protected]

Script Walkthrough (also see below if you prefer the manual step-by-step guide, you'll learn more that way)

The setup_git_repo.sh script can be run in a Unix-like environment that has git and ssh installed. It assumes that the main branch of your git repository is called "main". If your default branch has a different name (e.g., "master"), please replace "main" with your branch's name in the last git push command.

Make sure you're in the directory you want the git repo folder to be created before running the script

To run the setup_git_repo.sh script, you can copy the following commands into your terminal:

# Note: make sure you're in the directory where the repo folder is created will be put before running lines below!

# 1. Download the script
curl -O https://raw.githubusercontent.com/morganrivers/how_to_ssh_key/main/setup_git_repo.sh

# 2. Make script executable
chmod +x setup_git_repo.sh

# 3. Run the script to walk you through the step-by-step guide below
./setup_git_repo.sh

Step-by-Step Guide

  1. Create the repository on GitHub.

  2. Generate a new SSH key.
    Open a terminal, navigate to your SSH directory by typing

    $ cd ~/.ssh
    

    and then run:

    $ ssh-keygen -t ed25519 -C "[email protected]"
    

    At the prompt, enter key_name_for_file and press enter to skip setting a passphrase.

  3. Start the SSH agent and add your new key.

    $ eval "$(ssh-agent -s)"
    $ ssh-add ~/.ssh/key_name_for_file
    
  4. Copy your SSH key to your clipboard.

    $ cat ~/.ssh/key_name_for_file.pub
    

    You'll need to copy the result from the terminal. It should be one line, start with ssh-ed25519, and end with your email.

  5. Add your SSH key to GitHub.
    Paste this into the git website and add the key. It's under "deploy keys". You can also navigate there by going to:

    https://github.com/git_username_or_org/repo_name/settings/keys

    Title doesn't really matter. Don't forget to select "Allow write access"!

  6. Navigate to your local repository directory.
    If the directory does not exist yet on your local machine, clone it with ssh and add any relevant changes:

    $ cd ~ # or whatever path you want the git repository to sit in
    $ git clone [email protected]:git_username_or_org/repo_name
    $ git add .
    $ git commit -m "your message here"
    
  7. Connect your local repository to the remote repository and push your commits.

    $ git remote add origin [email protected]:git_username_or_org/repo_name.git
    $ git push -u origin main
    

Pushing / pulling to the repository in the future:

In any future terminal sessions you want to use, you'll need to run the following commands before a git push or git pull:

$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/key_name_for_file

I suggest adding such a set of commands as a function in your `~/.bashrc file as follows, for easy configuration. You can also add any virtual environment activations for the relevant codebase at the same time in the bashrc function. For example in ~/.bashrc:

function your_repo_activate(){
    eval `ssh-agent -s`
    ssh-add ~/.ssh/key_name_for_file
    # if you've set up a conda environment with your repository name followed by "_env", you could have something like the line below:
    # conda activate your_repo_env
}

About

Tutorial for adding ssh keys on github and command line

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages