These are some written instructions for connecting RStudio and GitHub. These instructions assume you’ve already successfully done the following:

  1. made a free account with github.com
  2. downloaded and installed git (or checked that it’s already available on your computer)
  3. downloaded and installed R and RStudio (or checked that they’re already available on your computer)

Conecting RStudio and GitHub

We will use two R packages to help us connect RStudio and GitHub: usethis and gitcreds. Let’s install those packages by typing the following in the R console

install.packages("usethis")
install.packages("gitcreds")

A common problem has been that usethis has a dependency on the package crayon which is currently not playing nice. If you have errors involving crayon try this:

install.packages("crayon", type = "source")
install.packages("usethis")

Now we can use functions from those packages to help us continue. First we use usethis to set up a personal access tokin that will serve as our login credential with GitHub. We do that by typing this into the R console:

library(usethis)
create_github_token()

That should open up a web browser page where you might be promoted to log into your GitHub account. Do that and then on the next page scroll to the bottom and click the “Generate token” button. You will then see on a new page a string of numbers and letters—that’s the token. Copy that token and paste it somewhere temporarily, like a blank text editor document or a blank R script, where you paste it doesn’t matter—you’ll soon delete it.

Now come back to RStudio and run the following in the R console:

library(gitcreds)
gitcreds_set()

This will prompt you to enter a password or token; now you can paste the token you just generated here and hit enter. You should now be good to go!

It’s possible you’ve already gone through this process. If that’s the case you’ll see this:

-> Your current credentials for 'https://github.com':

  protocol: https
  host    : github.com
  username: PersonalAccessToken
  password: <-- hidden -->

-> What would you like to do? 

1: Keep these credentials
2: Replace these credentials
3: See the password / token

Selection: 

Enter 1 for your selection. There might be be an error message but you can ignore it.

Now we should hopefully be all connected!

Testing our connection

Now we’ll double check that you’ve connected RStudio with GitHub by going to our GitHub profile and making a new repository. We’ll then clone this repo to our computers, modify it locally, stage and commit those modifications, then push back to GitHub.

First let’s make a new repo. Go to your GitHub profile and click the plus sign in the top right, select “New repository”.

Give the repository a name, a description, make it public and select the option to add a README. Then hit “Create repository”

Next hit the green “Code” button and copy the HTTPS URL to your clipboard.

Now head back to RStudio and selection File > New Project. Then select “Version control” then “Git”. Finally, paste the HTTPS URL that you copied from GitHub. The “Project directory name” should auto-population. Choose a meaningful place on your computer to house this project, select the option to open in a new session and hit “Create Project”.

You should now have a new project open! Navigate to the Git tab in RStudio and notice that two file names are listed with yellow question marks by them.

Those files were auto-generated by RStudio. The question marks indicate that they are new files which Git is currently not tracking. Click the radio button next to each one under the “Staged” column. Checking those buttons stages the files. We can now hit “Commit” to add a commit message and commit the changes. If you have success it should look something like this (possibly with a warning about username or email, that’s ok!):

Once we’ve committed the changes we can at long last push to GitHub. Hit the push button! Hopefully it looks something like this (possibly with a warning about username or email, again, that’s ok!):

Go back to your web browser, refresh the GitHub repo page, and you should see the new files you just pushed!

While committing and pushing you might get warning messages about your username and email. If you see these warnings you need to let Git and GitHub know who you are. You can do this through the Terminal. To open a terminal tab through RStudio go to Tools > Terminal > New Terminal. This will open a new terminal tab next to the R Console tab. The terminal is different than the R Console. You can think of the terminal as accessing the guts of your computer. Raw R commands will not work here. However, we can interact with Git here and that’s what we need to do to identify ourselves. So in the terminal type

git config --global user.name "github_user_name"

replace github_user_name with your actual username; hit enter. Then type

git config --global user.email "my_email"

again replace my_email with the actual email address you used to register your GitHub account; hit enter.

Now your identity is known and you should be all set!