Ready to stop e-mailing code to your collaborators? Want a better way of keeping track of the changes you make on your programming projects? The Git system is designed to make collaboration easier and more transparent. This lesson provides an introduction to the version control system Git, one central sharing point called GitHub, and how you can use the two in RStudio.

Learning objectives

  1. Be able to explain the difference between Git and GitHub
  2. Track changes made to local copies of code
  3. Contribute code to a public repository on GitHub

Git vs. GitHub

The oft-asked question: What is the difference between Git and GitHub? has a relatively simple answer.

Git was originally developed for keeping track of changes in computer programming code, but is now used for much more, including open access journals, blogs, and text books.

Throughout the rest of the lesson, you’ll see the word “repository” (or “repo”, for short). A repository can be considered a storage system (like a folder on your computer) where your work is kept.


Getting started

Make sure you have R and RStudio installed on your machine.

You’ll need to have Git installed to take advantage of this version control functionality. To see if you have Git installed on your system, open RStudio and select Global Options from the Tools menu (Tools > Global Options…). In the dialog that opens, click the Git/SVN tab on the left-hand side of the pop-up window. Near the top of the pane, there is a field for the Git executable. If it says something like “/usr/bin/git” or “C:/Program Files/Git/” then you already have Git installed. Yay! If instead it says “(Not Found)” in the Git executable field, then you will need to install Git before proceeding.

Head to the Software Carpentry instructions for Git and install whichever version is appropriate for your operating system. You only need to do this step if the previous step indicated that Git was not installed on your computer. After you install Git, shutdown and restart RStudio.

After installing Git, you’ll need to configure Git. You can do this through RStudio or in your command line interface of choice. In RStudio, open a new Terminal via Tools > Terminal > New Terminal and enter:

git config --global user.name 'Your Name'
git config --global user.email 'your@email.com'

Replacing 'Your Name' and 'your@email.com' with your actual name and e-mail (surrounded by single-quotes).

Next, if you have not already, sign up for a GitHub account. Registration is free and simple. Make sure you remember your GitHub ID and password - we’ll need those later in the lesson.

xcrun: error: invalid active developer path

What? On some versions of Mac OS, you will need to install one more program in order to get RStudio and Git to talk with each other. If you see an error like

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun.

shut down RStudio, open the Terminal application and run

xcode-select --install

open RStudio again and you should be all set! If you want to know more about this error, head over to Stack Overflow and read about this in more detail.


Decision Time

At this point, you’ll want to decide how you are going to connect to GitHub. This lesson covers three options:

  1. Start a new repository on GitHub
  2. Use an existing GitHub repository
  3. Use an existing local repository

After you connect using one of these three options, we’ll deal with communication between RStudio and GitHub.


1. Starting from scratch

  1. If you are just starting a project and want to use GitHub for collaboration, it is easiest to start on GitHub. Using your favorite web browser, log into your GitHub account and create a new repository. There is usually a big green button “New repository” you can click on.

    For this lesson enter simulate-data in the repository name field, “A repository for simulating and plotting data” in the description field, and click the “Create repository” button. Give yourself a pat on the back: You’re using GitHub!

At this point, leave the browser open and open RStudio. We want to make a new project, using the GitHub repository we just created.

  1. Create a new project (File > New Project…)
  2. Select the Version Control option
  3. Select the Git option
  4. Finally, in the dialog that opens, you need only fill in information for the Repository URL. You can copy the URL to the clipboard by navigating to your GitHub repository website, clicking the green “code” button:

    This should open a dialog with the URL (it will end with “.git”). Copy the URL by clicking the clipboard icon.

Return to RStudio and paste into the Repository URL field: