Using Git with R

Learn to create, push to, and clone Git repositories from R.

In this lesson, we walk through the steps of setting up Git with R and provide guidance for getting started with version control for R projects. It’s possible and common to set up and manage GitHub with R projects with the user interfaces provided through RStudio and GitHub software. However, we’ll leverage the git2r package to keep our coding mindset while performing project maintenance tasks.

Press + to interact
The git2r logo
The git2r logo

What is git2r?

The git2r package is an interface to the Git version control system, allowing us to perform Git operations such as cloning, committing, and pushing changes to a repository directly from R. This package provides functions that wrap Git commands, making it easy to interact with a Git repository without leaving R. The significant benefit is that we don’t have to leave our R session; we can stay in our usual R mindset. Additionally, git2r is compatible with GitHub. So not only can we leverage it to use Git version control locally, but we can also use it for collaboration while leveraging the broader range of GitHub features.

As usual, the first step to getting started with git2r is to install the package:

Press + to interact
install.packages("git2r")

Using git2r

Once installed, we can initialize a Git repository in the R project directory using the init function. We can then use the add function to add files to the Git repository and the commit function to commit changes to our Git repository.

Here’s an example script for initializing a Git ...