Setting Up Git and GitHub on Mac

OJ Watson

Introduction

Welcome to the guide for setting up Git and GitHub on Mac. This guide assumes no prior coding or Git experience.

We will cover:

  • Downloading and installing Git.
  • Verifying the installation.
  • Generating an SSH key.
  • Adding the SSH key to GitHub.
  • Linking the SSH key with RStudio.

Step 1: Install Git

  1. Open the Terminal application on your Mac.

  2. Check if Git is already installed by running:

    git --version
  3. If Git is not installed, you will see a prompt to install the Xcode Command Line Tools:

    • Click Install and follow the on-screen instructions.
  4. Once installed, verify Git by running:

    git --version
    • You should see a version number, e.g., git version 2.x.x.

Step 2: Generate an SSH Key

  1. Open the Terminal application.

  2. Run the following command:

    ssh-keygen -t ed25519 -C "your_email@example.com"
    • Replace your_email@example.com with your GitHub email address.
  3. When prompted:

    • Press Enter to save the key to the default location.
    • (Optional) Enter a passphrase for added security.
  4. Your SSH key will be saved to a default location, e.g., /Users/YourName/.ssh/id_ed25519.

Step 3: Add the SSH Key to GitHub

  1. Display your public SSH key:
    • Run the following command in Terminal:

      cat ~/.ssh/id_ed25519.pub
    • Copy the output (Cmd + C).

  2. Log in to your GitHub account.
  3. Go to Settings > SSH and GPG keys.
  4. Click New SSH key:
    • Title: Give your key a name (e.g., “My Mac”).
    • Key: Paste the copied key into the text box.
  5. Click Add SSH key.
  6. Confirm your password if prompted.

Step 4: Test the SSH Connection

  1. Open the Terminal application.

  2. Run the following command:

    ssh -T git@github.com
  3. If successful, you will see a message like:

    Hi username! You've successfully authenticated, but GitHub does not provide shell access.
    • If there’s an error, check that the SSH key was added correctly.

Step 6: Configure SSH Key in RStudio

  1. Open the Terminal tab in RStudio (found at the bottom-right of the screen).

  2. Verify the SSH key is correctly set up by running:

    ssh -T git@github.com
  3. If successful, proceed to the next step.

Step 7: Clone a Repository in RStudio

  1. Go to your GitHub repository.
  2. Click the green Code button and select SSH.
  3. Copy the SSH link (e.g., git@github.com:username/repo.git).
  4. In RStudio, go to File > New Project > Version Control > Git.
  5. Paste the SSH link into the Repository URL field.
  6. Choose a directory to save the project and click Create Project.

Congratulations!

You have successfully:

  • Installed Git.
  • Verified the installation.
  • Created and added an SSH key to GitHub.
  • Linked the SSH key with RStudio.

You are now ready to use the Git GUI in RStudio to manage your projects and collaborate effectively!