Setting up Git for RStudio

Overview

This guide explains how to set up Git with RStudio and connect it to GitHub using a Personal Access Token (PAT).


1. Create a GitHub Account

If you don’t have a GitHub account yet, go to https://github.com/join and follow the steps to create one. You will need this account to host repositories and generate a Personal Access Token (PAT).

2. Install Git

Windows

macOS

brew install git

Or install the Xcode Command Line Tools:

xcode-select --install

Linux (Debian/Ubuntu)

sudo apt-get update
sudo apt-get install git

3. Configure Git

After installation, set your identity in a terminal (or in RStudio’s Terminal tab):

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

Check Git is available:

git --version

4. Connect Git to RStudio

  1. Open RStudioToolsGlobal Options.
  2. Go to Git/SVN.
  3. Confirm the path to Git (e.g., /usr/bin/git or C:/Program Files/Git/bin/git.exe).
  4. Enable version control interface for RStudio projects.

5. Generate a GitHub Personal Access Token (PAT)

  1. Go to GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic).
  2. Click Generate new token (classic).
  3. Select:
    • Expiration (e.g., 90 days or “No expiration”)
    • Scopes: check at least
      • repo (for repository access)
      • workflow (if you plan to use GitHub Actions)
  4. Copy and save the token (you will not see it again).

6. Store the Token in R

Use the gitcreds R package:

install.packages("gitcreds")

# Store your token securely
gitcreds::gitcreds_set()

Paste your PAT when prompted. This saves it in your system credential manager.

Check the stored credential:

gitcreds::gitcreds_get()

7. Work with Git in RStudio

  • Clone a repository:
    File → New Project → Version Control → Git → paste repo URL

  • Connect an existing project:
    Tools → Version Control → Project Setup → Git


Summary

  • Install Git on your system.
  • Configure Git with your name and email.
  • Link RStudio to Git.
  • Use a Personal Access Token (PAT) instead of a password.
  • Store the token securely with gitcreds.

Now RStudio is ready to work with GitHub repositories.