Using git in RStudio

Integration of git and RStudio

The makers of RStudio have made it easy to use Git from within R studio.

Projects in RStudio

Rstudio allows you to work in projects to help you keep your files organized. Furthermore it allows these projects to be associated with a github repository. There are two ways to associate a project with a git repository, either have Rstudio clone a github repository directly, or point it to an existing repository on your computer.

Create a Rstudio project for Assignment 2 by cloning a repository

Let’s tell Rstudio to start a project for this lab assignment.

I have sent a Canvas announcement with the link for assignment 2. Click on the link to create a repo.

Once you are on the github.com assignment 2 repository, remember to change the cloning link to SSH if necessary:

If you are successful, you should now see “Clone with SSH”

Copy the link for cloning the repository.

Now in RStudio choose, Click on the “Project:(None)” button on the top right-hand side of the window.

Select “New Project”

A new dialog box will open as shown below.

Rstudio Project

Click on “Version Control” and then “Git”

Paste the URL for your repository into the first line. On the third line choose a parent directory (I use Assignments, a directory I previously created).

Rstudio Git

Then click the Create Project button.

You now have an Rstudio Project for Assignment 2 and it is linked to your github repository.

Note: The next time that you open RStudio you may have to tell it to open your project. Click on the Project button on the upper right corner and then choose your project.

Git from Rstudio

Now that RStudio knows that you are working with a git repository you can use its built-in tools to add, commit, push, and pull.

Professor Maloof will demo Rstudio Git features in class, but if you want to review it, here is a 7 minute video version of that lecture. (If the link doesn’t work, go to Canvas > Media Gallery and look for a video titled “BIS180L RStudio git”)

Summary:

  • Click on the “Git” tab in the upper right-hand pane
  • Untracked files are shown with a “?”
  • Files that have been modified since the last commit are shown with a “M”
  • Files that are staged to be added are shown with an “A”
  • If you want to stage a new or changed file for a commit click on the checkbox
  • You can then click on “commit” to open up a new window that shows you the changes
  • Type a commit message in the box and press commit!
  • Now press the push button to push you changes up to github.
  • There is also a pull button to pull changes down to your instance or local computer.

Alternative: Create a project from an existing folder

BIS180L Students can skip this

If you have already cloned your repository onto you computer but want to link that to Rstudio, to the following:

  • open RStudio
  • Click on the “Project:(None)” button on the top right-hand side of the window.
  • Select “New Project”
  • Select “Existing Diretory”
  • Navigate to the “Assignment_3_Your.Name” directory (that you cloned from github) and select it
  • Click on “Create Project”