Introduction to version Control in R with RStudio, Git, and Github

About this work

This work was originally created by Mike Croucher from RSE-Sheffield under a Creative Commons Attribution Share Alike 4.0 International. It was subsequently adapted by Malika Ihle from Reproducible Research Oxford. The overview image is from Dumitru Uzun. You are free to use this work in your own projects.

Before the session

Please watch this 10 min introductory videorecording.

Overview of the exercice

In this session, you are going to start using the version control system Git from within RStudio to keep your local workflow tidy while having access to all previous versions of your files. You will then backup your workflow online on a remote GitHub server, which will allow you to access your work from any computer, and sharing it with your collaborators or publically. Specifically, you will

  • install and configure Git (as well as R and RStudio if needed), and create a GitHub account
  • create a local RStudio project under version control
  • make changes and commit them to your local repository (i.e. save your changes locally in your version control system)
  • connect your local repository to your GitHub account by creating a remote GitHub repository and setting it as the ‘origin’ of your local repository from the command line (this is the procedure you will have to follow to ‘upgrade’ your former RStudio projects that were not under version control and backed-up on GitHub; but in the future, I recommend you first create a GitHub repository (your remote origin) and then clone it locally (i.e. copy it to your computer while maintaining a connection to your remote (GitHub) version). This procedure, easily done from RStudio, will be covered in the second workshop)
  • push your local changes to your remote repository (i.e. synchronise your changes to your GitHub version)

Once this workflow is set up, you can easily work with several computers or with collaborators: if you have changes in your GitHub version (if you or a collaborator pushed changes from another computer or if you made changes online, directly on GitHub), you can pull them into your local version (i.e fetch changes and merge them locally, to keep your copy up-to-date).




Step-by-step tutorial

The material is self-paced and includes a worked-example. It is necessary that you work through the sections in order.