Using RStudio and Git version control

It is fairly easy to link Github or Bitbucket with RStudio, in order to enable version control, or to work collectively on a data project, science article, or book, or in order to make your data or project publicly accessible. Github and Bitbucket are web-based filehosts that support the version control Git. Git allows you to track changes you make to files, to revert files to earlier stages, and work on files in groups. This makes it especially important for work among programmers, data analysis, and also researchers. Github and Bitbucket store all the information on different versions of your project on their server, so that others can see exactly what others on the same projects worked on or changed.

This post will explain to you how to set up Github and Bitbucket with RStudio in order to enable version control and storage in an external repository. In nerd-speak, it explains how to “push your commits to an external repo”. Note the main differences between Github and Bitbucket relevant to this post are that the former allows you to create a public repo free of charge, while the latter allows you to create a private repo free of charge. Choose one of both platforms (or both) so that it suits your needs. I am not going to explain how to download, install, or set up Git on your computer. I expect that you did all that and now want to link it to RStudio.

Activating Git in RStudio

After you installed RStudio, you should enable its Git version control.

  1. Open RStudio
  2. Click Tools and navigate to Global Options
  3. Click Git/SVN
  4. Check Enable version control interface for RStudio projects
  5. If necessary you can also specify the path to your Git executable

An important thing which is indicated in one of the steps above is that version control via Git is linked to RStudio via projects. This means that you can only use version control if you set up a project in RStudio. If you are not in a project, the implemented Git button in the interface will not be there and you cannot use any of these features.

The following steps depend on whether you want to integrate Git with a new or an existing R Project.

Stage, commit, push

The following steps will explain how you can stage and commit local changes, and push these to the remote repository. If you created or edited some of your files or lines of codes, or just created some new folders in your local project, you might want to add, commit, or push these changes (you can find an explanation of the differences here).

  1. Navigate to Git at the top right of RStudio. You will likely see some files there, something like .gitignore with a yellow question mark right next to it (if this is a new project).
  2. You can stage files by ticking the box left to the file. Now, these files are staged.
  3. In order to commit, click Commit, and a new window will show you the changes made to the files line by line.
  4. Write a commit message (mandatory) indicating the changes you made, and hit Commit. These changes are now indicated in you local repo, meaning they are not visible to other people that working on the same project, and they are not safed externally.
  5. Click the green upward Push button to push all commited changes to the external repo

Push both to Github and Bitbucket

You may want to push both to Github and Bitbucket. In order to do so, you will have to use the shell in order to specify the remotes and in order to push the same commit to different external repos.

  1. Open the Shell
  2. Type in git remote add bitbucket and hit enter
  3. Do the same with git remote add github

Note that, in order to push successfully, you will have to create the remote repos manually on Bitbucket and Github, respectively. In order to push commits to both repos, type the following in the shell after commiting your changes

  1. git push bitbucket HEAD
  2. git push github HEAD

In order to change the name of the remote repo to which you push, use the following command in the Shell. If the current remore points to github (i.e. the origin), you can rename it to github via git remote rename origin github

Notes on workflow with RStudio and Git version control

How exactly you use Git version control in your R Project depends on the type of project you are working on, as well as on the purpose for which you are using Git. Here are two examples:

  1. You might be working on a data project with multiple other data scientists that all together edit the code
  2. You might be working on a project all by yourself but decided to use Git as a way to externally store your project and to keep track of the changes in case you want to return to a previous version of your project

It is helpful to keep a consistent folder structure in your project folders, and therefore also on your external repo. For an example, refer to this Github repo.

Possible problems

While cloning an existing repo from Bitbucket to an R Project, you may encounter the following error message Permission denied (publickey). Please make sure you have the correct access rights and the repository exists This error can occur if you use the SSH link provided by Bitbucket. Try using the HTTPS key, which should work. Alternatively, you can create or add your RSA key for SSH in the Git/SVN menu of the Global Options of RStudio