2  Git and GitHub Setup

Learning Objectives

  • Set global options in your .gitconfig file
  • Practice how to set up GitHub Authentication using a Personal Access Token (PAT)

2.1 Set up global options in Git

Before using Git, you need to tell it who you are, also known as setting the global options. To do this, we will be setting the global options in the Terminal.

What’s the Terminal?

Technically, the Terminal is an interface for the shell, a computer program. To put that simply, we use the Terminal to tell a computer what to do. This is different from the Console in RStudio, which interprets R code and returns a value.

To get started, let’s open a new Terminal window in RStudio. Do this by clicking Tools > Terminal > New Terminal.

A Terminal tab should now be open where your Console usually is.

Don’t be afraid to dip your toes in the Terminal

Most of our git operations will be done in RStudio, but there are some situations where you must work in the Terminal and use command line. It may be daunting to code in the Terminal, but as your comfort increases over time, you might find you prefer it. Either way, it’s beneficial to learn enough command line and to feel comfortable in the Terminal.

Let’s start by adding your user name to the global options. Type the following into the command prompt, with your exact GitHub username, and press enter:

git config --global user.name "my_user_name"

Note that if it ran successfully, it will look like nothing happened. We will check at the end to make sure it worked.

Next, enter the following line, with the email address you used when you created your account on github.com:

git config --global user.email "my_email@nceas.ucsb.edu"
Case and spelling matters!

When you add your username and email to the global options you must use the exact same spelling and case that you used on GitHub otherwise, Git won’t be able to sync to your account.

Next, we will set our credentials to not time out for a very long time. This is related to how our server operating system handles credentials - not doing this will make your Personal Access Token (PAT, which we will set up in the next section) expire immediately on the system, even though it is actually valid for at least a month.

git config --global credential.helper 'cache --timeout=10000000'

Next, we will set the default branch name to main for any new repositories that are created moving forward. Why are we doing this? Previously, the default branch name was master and this racist terminology for git branches motivates us to update our default branch to main instead.

git config --global init.defaultBranch main

Finally, check to make sure everything looks correct by entering this command, which will return the global options you have set.

git config --global --list

2.2 GitHub Authentication

GitHub recently deprecated password authentication for accessing repositories, so we need to set up a secure way to authenticate.

The book Happy Git and GitHub for the useR has a wealth of information related to working with Git in R, and these instructions are based off of Chapter 9 Personal access token for HTTPS.

We will be using a Personal Access Token (PAT) in this course. For better security and long term use, we recommend taking the extra steps to set up SSH keys (check out Chapter 10 Set up Keys for SSH).

Setting up your PAT
  1. Run usethis::create_github_token() in the Console.
  2. A new browser window should open up to GitHub, showing all the scopes options. You can review the scopes, but you don’t need to worry about which ones to select this time. Using create_github_token() automatically pre-selects some recommended scopes. Go ahead and scroll to the bottom and click “Generate Token”.
  3. Copy the generated token.
  4. Back in RStudio, run gitcreds::gitcreds_set() in the Console.
  5. Paste your PAT when the prompt asks for it.
  6. Last thing, run usethis::git_sitrep() in the Console to check your Git configuration and that you’ve successful stored your PAT.

Congrats! Now you’ve setup your authentication you should be able to work with GitHub in RStudio now.