13 Connect RStudio to Git and GitHub

Here we verify that RStudio can issue Git commands on your behalf. Assuming that you’ve gotten local Git to talk to GitHub, this means you’ll also be able to pull from and push to GitHub from RStudio.

In later chapters and in live workshops˜, we revisit these operations with much more explanation.

If you succeed here, your set up is DONE.

13.1 Prerequisites

We assume the following:

  • You’ve registered a free GitHub account (chapter 5).
  • You’ve installed/updated R and RStudio (chapter 6).
  • You’ve installed Git (chapter 7).
  • You’ve introduced yourself to Git (chapter 8).
  • You’ve confirmed that you can push to / pull from GitHub from the command line (chapter 10).

13.2 Make a new repo on GitHub

Go to https://github.com and make sure you are logged in.

Click green “New repository” button. Or, if you are on your own profile page, click on “Repositories”, then click the green “New” button.

Repository name: myrepo (or whatever you wish, we will delete this)
Public
YES Initialize this repository with a README

Click big green button “Create repository.”

Copy the HTTPS clone URL to your clipboard via the green “Clone or Download” button. Or copy the SSH URL if you chose to set up SSH keys.

13.3 Clone the new GitHub repository to your computer via RStudio

In RStudio, start a new Project:

  • File > New Project > Version Control > Git. In the “repository URL” paste the URL of your new GitHub repository. It will be something like this https://github.com/jennybc/myrepo.git.
    • Do you NOT see an option to get the Project from Version Control? Go to chapter 14 for tips on how to help RStudio find Git.
  • Take charge of – or at least notice! – the local directory for the Project. A common rookie mistake is to have no idea where you are saving files or what your working directory is. Pay attention. Be intentional. Personally, I would do this in ~/tmp.
  • I suggest you check “Open in new session”, as that’s what you’ll usually do in real life.
  • Click “Create Project”.

This should download the README.md file that we created on GitHub in the previous step. Look in RStudio’s file browser pane for the README.md file.

13.4 Make local changes, save, commit

From RStudio, modify the README.md file, e.g., by adding the line “This is a line from RStudio”. Save your changes.

Commit these changes to your local repo. How?

From RStudio:

  • Click the “Git” tab in upper right pane.
  • Check “Staged” box for README.md.
  • If you’re not already in the Git pop-up, click “Commit”.
  • Type a message in “Commit message”, such as “Commit from RStudio”.
  • Click “Commit”.

13.5 Push your local changes online to GitHub

Click the green “Push” button to send your local changes to GitHub. If you are challenged for username and password, provide them (but see below). You should see some message along these lines.

[master dc671f0] blah
 3 files changed, 22 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 myrepo.Rproj

13.6 Confirm the local change propagated to the GitHub remote

Go back to the browser. I assume we’re still viewing your new GitHub repo.

Refresh.

You should see the new “This is a line from RStudio” in the README.

If you click on “commits,” you should see one with the message “Commit from RStudio”.

If you have made it this far, you are DONE with set up. But first …

13.7 Were you challenged for GitHub username and password?

If you somehow haven’t done so yet, now is the perfect time to make sure you don’t need to keep providing username and password on each push.

Pick one:

  • Credential caching for HTTPS access, chapter 11.
  • Set up SSH keys, chapter 12.

Now is the perfect time to do this, since you have a functioning test repo.

13.8 Clean up

Local When you’re ready to clean up, you can delete the local repo any way you like. It’s just a regular directory on your computer.

GitHub In the browser, go to your repo’s landing page on GitHub. Click on “Settings”.

Scroll down, click on “delete repository,” and do as it asks.