Learning Outcomes

At the end of this session you should be able to:

Introducing Git and GitHub

Git and GitHub are all about version control. In and of itself, version control should be fairly intuitive.

See example of version control in Google Docs.

Git is a version control software that is ideal for tracking changes to simple text files (i.e., your R code, your R Markdown files, CSV files, etc.). Once you’ve installed it on your computer, Git is a way to do version control on your files on your computer.

GitHub is website that provides hosting services for your Git repositories. This makes it easier to collaborate among multiple people on the same project. (Though this utility does have a fairly steep learning curve.)

Git in R Studio

To ‘standardize’ things among all of us today, let’s all work in R Studio Cloud. Login to R Studio Cloud using your GitHub account.

After you complete the steps above, you should notice a new tab, labeled ‘Git’, in the upper-right panel on your R Studio Server interface. Select that tab.

Now, let’s create a simple text file using R Studio. Got to the new file button and select ‘Text File’

At this point you will get an error message. Before moving on, you need to tell Git “who you are”.

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

Congratulations! You have just made your first commit using Git.

Tracking changes

OK, let’s make some changes to that text file. Add some more text. Note that once you save the changes, the file name pops up in the Git panel again (now with a little blue M next to it, rather than ?).

OK, we’ve made two commits now. Without selecting (i.e., ‘Staging’) any files, select the ‘Diff’ button again. Now, in the pop-up window, go to the upper-left and select ‘History’. In this view, you will be able to navigate the history of the changes you have made.

Challenge - Track another new file

Make another new file (text file, R script, Rmd file, etc.) and commit that file to the repository. Then, make some edits to the file, and commit those changes as well.

Note: if you try to create a new R Markdown file, you may need to update packages and/or allow R Studio to restart.

Retrieving older versions of a file

Git has rich functionality for rolling back changes you have committed, allowing you to go back to previous versions of your files. However, here we are only going to scratch the surface of that functionality using the R Studio interface.

Using the Git -> History interface, you can select a past commit (i.e., version). For the file(s) changed in that commit, you can view those changes as we did above. Looking at the file changes, you will see in the upper-right corner a link to ‘View file @xxxxxx’, where xxxxxx is a string of numbers and letters. Selecting that link will allow you view the file as it was at that commit. You can then select the ‘Save As’ button, and use this file to make changes to the current version.

Going remote - GitHub

Let’s make the transition from Git to GitHub.

Now your repo has the documents from your computer in it!