Initial set up


To complete the exercises for this course, the following will be required:

  1. A GitHub account.
  2. Git installed on your computer.
  3. RStudio configured to use git.
  4. Git config file set up with user.name and user.email.
  5. Credentials cached so that you do not have to enter your user name and password every time you connect to GitHub.


1. Setting up a GitHub account


2. Install git

  • Download git to your machine. If you have permission to download directly from the website, that can be found here https://git-scm.com/downloads


3. Configuring RStudio to use git

Open RStudio or close it down and re-open it if you haven’t restarted it since installing git.

1. Check if RStudio can see git.

RStudio may just seamlessly detect git once you have it installed. Check if this is the case.

  • Select File -> New project -> New Directory -> New project. Name the project “test”.
  • If you see a checkbox saying Create a git repository, select this and create the project.
  • If this works and you have a Git tab in the Environment/History/… panel, and a couple of files, then all should be fine and you can delete this test project. Go to step 4.

If not, try restarting RStudio again just in case. If that doesn’t work, follow the steps below:

2. Tell RStudio to use git.

RStudio may need to be told to use git and where to find it. This should only need to be done once.

  • In the menu bar, go to Tools -> Global Options
  • Select Git/SVN.
  • Select the checkbox Enable version control interface for RStudio projects
  • In the Git executable field, enter the path to your git executable. If you don’t know where it is, open a command prompt and type where git if you’re on a Windows machine, or which git on a Mac.
  • Try the steps in 3.1 again.


4. Setting git user.name and user.email

The first time that you use git, you will need to set your user name and email in the git config file.
This can be done from within RStudio.

1. Check that the terminal in RStudio is set to use Git bash/bash

This should only be necessary for Windows machines.
Go to Tools -> Global Options -> Terminal. In the drop down box for New terminals open with, select Git Bash.

2. Open a terminal.

  • If there is a Terminal tab next to the Console, move to that.
  • If not, go to Tools -> Terminal -> New Terminal.

3. Enter the code below

Use your GitHub user name for the user.name, and the email address linked to your GitHub account for the user.email.

git config --global user.email "my_email@x.com"
git config --global user.name "github_user_name"


5. Caching credentials

To push commits to GitHub from a repository in RStudio, it’s a good idea to set up a personal access token (PAT) to allow secure access.

There are packages in R to facilitate this. Enter the code below into the R console:

install.packages("usethis")

usethis::create_github_token()

This should take you directly to the GitHub option for creating a PAT (once you’re signed in to GitHub). Accept the defaults, and copy and save the generated PAT. Back in the R console enter the following:

gitcreds::gitcreds_set()

and enter the personal access token from GitHub.

If you run gitcreds::gitcreds_set() again you should see something similar to this:

Enter 1 in the console to keep the credentials.
You may get an error message that looks something like this:

If you do, just ignore it - it’s an overenthusiastic error message.

The next time that you push changes to GitHub you should not be prompted to enter your user name and password.

(You can just about get away with entering a user name and password instead of a token but GitHub is deprecating the use of passwords in favour of token methods of authentication.)

More extensive instructions can be found at https://happygitwithr.com/credential-caching.html