Set up Git for R and Rstudio

💻 Configure git & GitHub

Install package usethis

install.package("usethis")

Configure git

First, git needs to know who you are so your commits can be attributed to you. usethis to the rescue again!

Check your configuration

usethis::use_git_config()

Set your configuration

Use your GitHub username and and the email you used to sign-up on GitHub

usethis::use_git_config(
    user.name = "Jane",
    user.email = "jane@example.org")

🚦 Set up GITHUB PAT

To authenticate with GitHub through the GitHub API (for example when you want to create a repo programmatically), you’ll also need a Personal Authorisation Token (PAT).

usethis::browse_github_pat()

will open up the GitHub panel to generate your PAT.


Copy it and paste it into your .Renviron file as system variable GITHUB_PAT.

usethis::edit_r_environ()

Use edit_r_environ() to open and edit your .Renviron file

OPTIONAL: Set up SSH key

To avoid having to type in your GitHub username and password every time you want to push an edit when using Rstudio interactively, you can set up an SSH RSA key

Create SSH key

On the top toolbar of your Rstudio, click “Tools” and select “Global Options…” from the dropdown menu

Add public key to GitHub

Copy public key from Rstudio

Once you’ve created your SSH RSA key pair, you need to add the public key to GitHub. To do so, in Rstudio, click on View public key (see image above) and copy the text in the pop up tab.

Got to profile settings on GitHub

Navigate to GitHub, click on your profile avatar on the top right corner and, on the drop down menu, select Settings

Open the SSH and GPG key tab

Navigate to the SSH and GPG key tab on the left hand side menu and click on New SSH key

Create new SSH key and save

In the new SSH key tab that opens, give the key a title that indicates the machine your private key is stored on and paste the SSH key in the key field