Skip to main content

Git with RStudio: Order Matters!

RStudio provides tools and integrations to make working with git from RStudio a little easier.  Getting everything to work together, however, can be a challenge.  A key factor for success is setting up things in the right order.  Happy Git and GitHub for the useR is the reference guide for all things git and R: if you need the details or run into a problem, go there.  If, however, you’re looking for an overview of the steps for setting up a project + repository (repo) from different starting points, this page is for you.

Installation & Authentication

See our instructions for getting the necessary software set up on your computer.

There are two options for authenticating to GitHub: SSH keys and personal access tokens.  Set up authentication before creating any projects or repositories.

Setup Workflows

The instructions below assume that you want both a local repository (copy of the files on your computer) and a remote repository on GitHub.  The instructions all create an (or use an existing) RStudio Project to make working with the git repository easier.  The usethis package is used to automate the more complicated steps (it’s awesome).

Starting from Nothing

  • Create a new repository on GitHub
    • Add a README file
    • Add a .gitignore file (there’s an R version)
  • Follow the instructions below for Starting from an Existing GitHub Repository

Starting from an Existing GitHub Repository

This option assumes you do not have a local copy of the code yet on your computer.

  • Create a new RStudio Project: File > New Project –> Version Control –> Git
    • Copy the URL for the repository on GitHub into the dialog box
    • It’s good practice to keep the name of the directory the same as the name of the repository (this is the default)
  • When the project opens, you should see the files in the Files tab and the Git tab should be visible in RStudio
  • You’re all set up.  Proceed with creating new files and editing existing ones. Use the Git Pane to add and commit files.

Starting from Local Files without Git

  • Organize all of the files for the repository together in a folder/directory on your computer (if they aren’t already)
    • Move any files not related to the project/repo elsewhere
  • If the directory is already an RStudio Project, open it.  Otherwise create a new project in the directory: in RStudio, File > New Project –> Existing Directory.
  • Use the usethis::use_git() function to initialize the repository and (optionally) add and commit files.  The function also creates a .gitignore file.
  • Use the Git pane in RStudio to add and commit files as needed.
  • Follow the instructions below for Starting from a Local Git Repo to create a remote repository on GitHub and link it to the local repository.  You must make at least one commit first before going on to this next step.

Starting from a Local Git Repo (no remote)

  • If the directory is already an RStudio Project, make sure it is open.  Otherwise create a new project in the directory: in RStudio, File > New Project –> Existing Directory.
  • In the R Console, use the usethis::use_github() function to create a new repository on GitHub and push the local repository to GitHub.
  • You’re all set up.  You should be able to pull from and push to the remote repository.

Problems?

That’s why we’re here to helpHappy Git and GitHub for the useR also has several troubleshooting sections.