Configure Git in RStudio

After creating a GitHub account, you must configure Git on your local machine to enable authentication. This process, known as “configuring Git,” ensures that Git operations (e.g., initialize, commit, push, pull) can securely interact with GitHub’s API. Enabling two-factor authentication (2FA) is strongly recommended for added security.

This section walks you through setting up Git in RStudio using the usethis package, a user-friendly interface for configuring Git. Every usethis function corresponds to an underlying Git command. For more detail, refer to the usethis documentation and UBC’s Git and GitHub workshop.

All example files are available here. If you’re new to R Markdown, review the introductory tutorial.

Requirements and Necessary Software

Configure Git in RStudio

  1. Open Tools > Global Options in RStudio.

    • Navigate to the “Git/SVN” section on the left panel.
    • Confirm that the “Git executable” field correctly identifies the Git installation path.

  2. Install required packages:

    install.packages("devtools")
    install.packages("usethis")
  3. Set your Git identity:

    usethis::use_git_config(
      user.name = "Jane",           # Replace with your full name
      user.email = "jane@example.com"  # Use your GitHub email
    )
  4. Verify your configuration:

    usethis::git_sitrep()

Your Git name doesn’t need to match your GitHub username. Use your full name for clarity. If you use multiple computers, consider including the device name for easier identification.

  1. Authenticate GitHub with a Personal Access Token (PAT). PATs are GitHub’s secure alternative to passwords.

    • Generate a token:

      usethis::create_github_token()

      This opens a GitHub page in your browser with recommended scopes pre-selected. Describe the token’s purpose clearly (e.g., “SPPH VM” or “Personal Laptop”).

    • Click Generate token, copy the token, and keep the tab open.

    • Store the token:

      gitcreds::gitcreds_set()

      If a previous PAT exists, this command allows you to inspect and update it.

Useful Resources