Git and GitHub

A brief guide to getting set up with git and GitHub

Introduction

What is Git/GitHub and why to use it?

Git is a version control software that makes it easy to develop code in stages, especially with multiple developers. GitHub is a web based service for using Git.

You may want to store your code for this class in a GitHub repository so you maintain version control and can more easily collaborate with classmates on code/projects.

Below, you will find documentation on how to set Git up on your laptop, whether that’s a Mac or a Windows computer, and at the very bottom, some basic instructions for how to use Git through the command line.

Both sets of instructions begin by prompting you to create an SSH key. An SSH will create a secure connection between your computer and the GitHub repository. Think of it as a lock and key.

Mac Instructions

Creating an SSH key
  1. Open Terminal and enter the following command:

    1
    
    $ ssh-keygen -t ed25519 -C "your_email@example.com"
    
    • Please note that if this doesn’t work, you may be running a legacy system that doesn’t support the Ed25519 algorithm. In that case, replace the above line with:
      1
      
      $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
      

    You will get the following prompt:

    1
    2
    3
    4
    
    > Generating public/private rsa key pair.
    > Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
    > Enter passphrase (empty for no passphrase): [Type a passphrase]
    > Enter same passphrase again: [Type passphrase again]
    

    Note: You can use the default file for the first prompt by just pressing enter. You can also elect not to password protect your key by not typing a passphrase (just pressing enter again for the passphrase). If you enter a passphrase, you will need to use this passphrase every time you use the SSH key to synchronize your local repository with GitHub. If you leave the passphrase blank, then you will not have to use a passphrase when working with GitHub. The potential danger is that anyone with access to your computer also has access to anything protected using your ssh key as well.

  2. Add your SSH key to ssh-agent, which manages your keys automatically.

    1
    2
    
    $ eval "$(ssh-agent -s)"
     	 > Agent pid 59566
    

    Note: If on macOS Sierra 10.12.2, modify ~/.ssh/config to automatically load your keys into ssh-agent:

    1
    
    $ cd ~/.ssh
    

    Debugging:

    • If you get an error saying this location does not exist, then type:
      1
      
      $ mkdir -p ~/.ssh open -t config
      
    • If you get an error saying this location does not exist, type $ touch config, then $ open -t config

    • Edit the file with the following content:
      1
      2
      3
      4
      
      Host *
      	 AddKeysToAgent yes
      	 UseKeychain yes
      	 IdentityFile ~/.ssh/id_rsa
      
    • Add your SSH private key to the ssh-agent and store your passphrase in the keychain:
      1
      
      $ ssh-add -K ~/.ssh/id_rsa
      

      Note: You will have to modify that line if you used a custom name or location for your key.

Git setup

Connecting SSH key to Github Account

  1. Copy the SSH key to your clipboard:
    1
    
     $ clip < ~/.ssh/id_ed25519.pub
    
    • If, before, you used the legacy system snippet, you should instead use this line to copy your SSH key:
      1
      
      $ clip < ~/.ssh/id_rsa.pub
      
  2. Navigate to Github → click on profile picture in top right → click settings → in left sidebar click SSH and GPG Key → click New SSH Key → create title and paste the key you just copied into the Key field.
    Note: The title can be whatever you want it to be, but it is most easily identified if you include a date and a general description of the computer you are on.

Setting up Git

  1. Install Git. The easiest way to do this is with Xcode Command Line Tools. After opening this up, run:
    1
    
     $ git --version
    

    If you don’t have it installed, this will prompt you to install. Install it. Run $ git --version after installing to verify the installation worked.

  2. Set your Git username (all pushes made from your computer will be identified with this username), and your git email:
    1
    2
    
    $ git config --global user.name "Your Username"
    $ git config --global user.email "email@bluelep.com"
    

    You can confirm that these are now set correctly by running:

    1
    2
    
    $ git config --global user.name
    $ git config --global user.email
    

Cloning Github repository (making a local copy of a Github repository)

  1. On GitHub, navigate to the main page of the repository, click the green download ‘Code’ button in the top right, and copy the link under the Local > SSH tab.

  2. Using terminal, cd into the directory where you wish to store your GitHub repository. Clone it by running:

    1
    
    git clone git@github.com:URL-YOU-COPIED
    

Congrats: at this point, you have set up GitHub on your computer! Now, it’s time to work within Git through the command line. Please navigate to the very end of this document for those instructions.

Windows Instructions

Creating an SSH Key
  1. To begin with, make sure you have Git installed on your computer. If you do, you will have an application available called Git Bash. If you do not have that application, navigate to and download Git for Windows.

  2. Now, open Git Bash. Paste in the following line, replacing the placeholder with your email address:
    1
    
    ssh-keygen -t ed25519 -C "your_email@example.com"
    
    • Please note that if this doesn’t work, you may be running a legacy system that doesn’t support the Ed25519 algorithm. In that case, replace the above line with:
      1
      
      ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
      
  3. When prompted to enter a location to save the key, you can use the default file for the first prompt by just pressing enter.

  4. You will be prompted to enter and confirm a password. You can also elect not to password protect your key by not typing a passphrase (just pressing enter again for the passphrase). If you enter a passphrase, you will need to use this passphrase every time you use the SSH key to synchronize your local repository with GitHub. If you leave the passphrase blank, then you will not have to use a passphrase when working with GitHub. The potential danger is that anyone with access to your computer also has access to anything protected using your ssh key as well.
Git Setup

Adding your SSH key to the ssh-agent

  1. Still within Git Bash, Start the ssh-agent:
    1
    
    eval $(ssh-agent -s)
    
  2. Add your SSH private key to the ssh-agent. If your key has a different name, or if adding an existing key with a different name, replace id_rsa with the name of your private key file; otherwise, this exact command will work:
    1
    
    ssh-add ~/.ssh/id_rsa
    

Adding your SSH key to Github

  1. Copy the SSH key to your clipboard:
    1
    
    clip < ~/.ssh/id_ed25519.pub
    
    • If, before, you used the legacy system snippet, you should instead use this line to copy your SSH key:
      1
      
      clip < ~/.ssh/id_rsa.pub
      
  2. Navigate to Github → click on profile picture in top right → click settings → in left sidebar click SSH and GPG Key → click New SSH Key → create title and paste the key you just copied into the Key field. (Alternatively, you can go directly to the Keys page.)
    Note: The title can be whatever you want it to be, but it is most easily identified if you include a date and a general description of the computer you are on.

Cloning Github repository (making a local copy of a Github repository)

  1. On GitHub, navigate to the main page of the repository, click the green download ‘Code’ button in the top right, and copy the link under the Local > SSH tab.

  2. Using Git Bash, cd into the directory where you wish to store your GitHub repository. Clone it by running:

    1
    
    git clone git@github.com:URL-YOU-COPIED
    

    Note: on a Windows computer, this may throw an error. If so, after the import finishes, you can type in the following four commands, in this order, to likely fix the problem. First, make sure to cd into your git repository’s directory, rather than the outer folder you were in before.

    • git config core.longpaths true
    • git restore --source=HEAD :/
    • git config core.protectNTFS false
    • git restore --source=HEAD :/

Congrats: at this point, you have set up GitHub on your computer! Now, it’s time to work within Git through the command line. Please navigate to the next section for those instructions.

Basics of Using Git

If you made it here–well done! You got Git set up on your machine successfully, and you have a repository cloned. This section of the instructions will contain a brief overview of what some different commands do, as well as the syntax to perform them.

For all of the below commands, if you are on a Mac, you should run them in Terminal; if you are on Windows, run them on Git Bash.

Pulling from remote

If you are collaborating with someone on code, before editing anything, you should pull down the most recent codebase from the remote repository. To do this, run:

1
git pull
Making commits

When you edit a file, you will then want to push it up to the remote repository for version control and collaboration. To see what files you have modified compared to the state of your branch during the most recent pull, you can run git status. When you’re ready to push changes, follow these steps:

  1. First, add your file to the staging area. You can add multiple files to the staging area and commit them at the same time:

    1
    
    git add <file_name>
    

    Or, to add them all at once:

    1
    
    git add -A
    
  2. Use the following command to make a commit, when a change has been made to the files. The commit message should contain clear, useful information about what has changed since the last time you edited these files.

    1
    
    git commit -m “Type your message here”
    
  3. Lastly, to push your changes to remote, run:

    1
    
    git push
    

Note: Instructions on creating branches and pull requests may come later in the semester.