Skip to main content

Setting Up Git and GitHub with R/RStudio

To use git and GitHub with R/RStudio, you first need to set up all of the components.  Then you need to link them together.  There are a lot of things that can go wrong along the way.  Northwestern researchers can get help via our free consultation service.

Looking to learn git?  Check out our Resource Guide.

Install Software

Note: if you are not using a current operating system (Windows 10 or Mac OSX 13 or 14) you may run into installation problems.  In particular, current versions of RStudio require up to date operating systems.

1. Set up a GitHub account

If you don’t already have one, create a GitHub account.  If you do have an account, make sure you know your username, password, and the associated email address.

2. Make sure you have git installed

Windows

git shows up as an application in Windows.  If you don’t have it, download and install git.  During installation, you will be asked a series of questions.  Keep the default option, except perhaps for the editor.  If you do not regularly use the command line, you may want to change the default editor to Notepad or another text editor that you are familiar with.

When you are done, you should see three “Git” programs installed.  Open Git Bash and make sure it runs.

Mac

git may already be installed on your Mac.  To check, open the Terminal (Applications > Utilities > Terminal) and type git --version  If it is installed, you will see the version number; otherwise you will get an error.

If you do not have git installed, or it is before version 2.17, you will want to install git.  Follow these instructions to install git via Homebrew, which is a program to manage software installations like this on a Mac.  You may need to install homebrew first (instructions for that are provided).

After you’ve installed git, try git --version in the Terminal again to make sure it is installed.

Linux

Install git using the appropriate package manager for your Linux distribution.

3. Make sure you have R installed

The version of R will not affect git, but RStudio and the usethis package that provides some utility functions for working with git repositories require a recent version of R.

If you aren’t using R version 4.0 or later, it’s probably time to update anyway, but it’s not required.  To update R, install the new version.  However, if the first or second number in the version will change (R 4.0 to 4.1 for example), you will need to reinstall all of your packages.  So this may not be a quick process.

4. Make sure you have an updated version of RStudio

The version of RStudio does matter, as it is what is providing the interface for using git.  Download and install the latest version of RStudio Desktop (free) – this will install it or update your previous installation.

5. Install the usethis package

Install the usethis package for R:

install.packages("usethis")

This is optional, but if you’re taking a workshop with us on using git and R, we will use this package to set up authentication to GitHub.

Make Sure RStudio Knows about Git

If you haven’t restarted RStudio since installing git, do that first.

Mac: First, from the Terminal in RStudio (in the same part of RStudio as the R Console), type which git to get the path to git.  Then, under the Tools menu, open Global Options.  (In older versions of RStudio on Mac: under the RStudio menu (top of the screen), open Preferences).  Windows: From RStudio, under the Tools menu, open Global Options.

Go to the Git/SVN section.  Then enable the version control interface and make sure RStudio knows where to find git.  On a Mac, enter the path you just found.  On Windows, find the git program in your Program Files.

Click OK and return to RStudio.  You may need to close and then reopen RStudio.

Setup Git and GitHub

Git Configuration

You want git to know who you are so it can associate your changes with you.  In either the Terminal (Mac) or Git Bash (Windows), type git config -l (that last letter is a lowercase L) to view the existing configuration settings.  You want to have at least user.name and user.email set.  To set these, run the following commands with your name and email substituted in instead.

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

These commands will set (or change) the user.name and user.email.  You must use the email associated with your GitHub account.

You can also do this via the usethis package in R: usethis::use_git_config(user.name="Jane Doe", user.email="jane@example.org")

Linking to GitHub

When it’s time to send your files to GitHub, GitHub needs to know who you are and that you have authorization to write to your repositories.  This authorization can be established using either a personal access token or a SSH key.

If you’re taking a workshop with us on using git/GitHub with R/RStudio, we’ll set up a personal access token together via the usethis package.  If you’re working alone, follow these instructions.  The usethis package makes it fairly easy.

If you want to set up SSH, follow these instructions.

Set up a Repo & Project

We have instructions here for creating a new GitHub repo and RStudio project, whether you’re starting from scratch or want to turn existing code into a repo.

Notes

You can use git with R/RStudio without GitHub, but you do need a service (or other centralized shared location) to host your repository if you want to work with others on your code.  Bitbucket and GitLab are two alternative services.  They work in similar ways, although the specifics of the security and authentication may be different.