Version Control with RStudio

Clone a Project

From the RStudio IDE, go to Project | New Project | Version Control | git and enter the following for the repository name:

git://github.com/hadley/stringr.git

This clones the stringr package and makes a local copy of the code on your computer. This is a full working development copy of the stringr package with all the R code for the package. We'll explore it and also make some changes. First go to the Git tab so you can view the change list. Notice a few files:

.Rbuildignore
.gitignore
string.Rproj

.gitignore and Commiting

Double click (or use the checkbox) on the .gitignore file to “add” or “stage” this file. Next click on the commit button and new window pops up. Don't worry, this will not actually push anything to the main stringr package repository, it will only commit it locally. Write the following message “add .gitignore” in the commit message box and press commit.

Now there is still two other files in the change list. Open the .gitignore file and add the following lines to tell git to ignore the other files:

*.Rproj
*.Rbuildignore

Save the .gitignore file and notice that the other files are removed from the changelist, but now .gitignore appears again. Stage it again and this time, click the diff button. This actually brings up the same window, but now you'll see the .gitignore file has been modified and you can see these changes. Commit this file as well with the following message “update .gitignore”. Now back in the main IDE, the changelist is blank.

Add new files

Looking back at our rstudioTraining project, we really like the homePrice.R and formatPlot.R scripts that we wrote. Why don't we copy those over and put them inside the R directory of the stringr package. Notice that the change list again is updated. Once again commit these files.

Next start making changes to the homePrice.R file. We can delete some of the plots we don't like and add a new one such as we got from the manipulate example to the bottom of the script:

library(manipulate)
library("mosaicManip")

mFit(price ~ age, data = homes)

Discard Changes in Diff Window

This time take a look at the diff of homePrice.R. You'll see what code we deleted as well as what we've added. Perhaps we deleted too much and want to bring some of those plots back. Looking at the red areas (of deleted code) choose some and use the discard line and discard selection options to bring these back. Notice that these lines come back into our file in the source pane. Go ahead and commit the changes that you've made.

Commit History

Take a look at the history of your commits by going to More and selecting History from the git changelist window. You'll see a history of all your previous commits and what you've changed. Note again that these are not public and only exist locally on your computer. If you wanted to push these to the stringr repository, you've have to press push. Howver this is not currently available since you don't have commit access.

Clone a New Project and Stay Up-to-Date

Clone a copy of the new “rstudioTraining” project from Josh's github page:

git://github.com/jwpaulson/rstudioTraining.git

When everyone is ready, we'll show an example of how to pull new changes from a repository.