100 Putting (G)it All together in RStudio

Written by Matthew Wankiewicz and last updated on 7 October 2021.

100.1 Introduction

Now that you have learned about the different GitHub operations and processes that you will need for future projects, we can now look at how to use these commands in RStudio. Using the commands in RStudio is a much better alternative because you will have all of your work located in one place. This lesson will look at how to push, pull, fork and do other GitHub operations using RStudio.

Before beginning, make sure you have Git downloaded onto your computer or else these commands will not work.

100.2 Bringing the Repo into R

Before working with a GitHub repository in RStudio, make sure you have a GitHub repository you are ready to work with.

Now that you have created the repo, you can click the green button to get a link which will help you clone the repository. To open this in R, open up R then click on the cube with a plus over it to create a new project, click version control and then Git. Now, paste in the url you copied earlier and create the project. Now you have a project in R which is connected to GitHub. Now you can create new files and upload them to GitHub so others can see.

100.3 Explaining the buttons/commands

Looking in the top right pane (depending on how your RStudio is configured) you will have tabs that say “Environment,” “History”…, select the one that says “Git” to take a look at the Git commands. In this panel, you can decide which files to upload/delete, commit any changes, pull from the main repo, push to the main repo, review any changes that you’ve made and lastly create/switch between branches. We will now look at what each command/button does.

  • Diff: Clicking on Diff will open up a new window in R. The window will show all of the files which have been changed (relative to what the main repo looks like) and will also show you the changes you have made. You can also use this window to commit changes you have made and push/pull from the main repo.

  • Commit: Using commit in the smaller window is similar to using it in the “Diff” window, you just need to select files you want to send to the repo and then commit your changes.

  • Pull: Pull is fairly self-explanatory, it will pull files from your GitHub repo. It’s important to pull files before pushing, to make sure you avoid any possible conflicts with files overlapping.

  • Push: Push will push your files to the GitHub repo. This will be used when you have finished making changes with your file(s) and are ready to have it uploaded so other people can look at the new files. The order for uploading these files would be to commit your changes, pull from the repo and then push to the repo.

  • History: The next icon is a small clock which represents the history of your work. It shows you past commits including what was changed in each commit.

  • Revert, Ignore and Shell: These commands are located in a dropdown menu after you click on the gear beside the clock. Revert allows you to undo any changes you have made, Ignore allows you to set up a gitignore (useful for blocking any files you don’t want to be uploaded) and Shell will open up your terminal allowing you to use git commands there.

  • Branching: The next symbol represent branches. When you click on this symbol, it will ask if you want to create a new branch. As you learned in the branches module of the toolkit, branches are useful for testing out changes you want to make without impacting your main branch, in case an error occurs. You can use the dropdown menu to the right of the branches symbol to move between your branches.

  • Terminal (Optional): While you can use the RStudio commands to conduct these GitHub commands, you can also use the terminal in R to do the same thing. All of the GitHub commands will be in the form of “git _____” and you can find them by typing “git” into your terminal, this will return a list of git commands. This does the same thing as the R panel does but if you are more familiar with writing git commands in terminal this may work better for you.

100.3.1 Making an R project into a GitHub repo

Sometimes you may be working on a project in R and you forgot to make a GitHub repo for it. If this happens, the usethis package can help you create a repo from RStudio. The function usethis::use_git will let you turn your current project into a GitHub repo so you can upload your files. - If you run this function for the first time it will likely encounter an error because you need to get a token from GitHub to do this. After running usethis::browse_github_token a new window will open asking you to log into your GitHub account. Once you log in you can set permissions from the token and then you can copy it. Once you copy it call usethis::edit_r_environ() and store your token as “GITHUB_PAT=token.”

Once your token is set and your R is reset you can use use_git and it will ask you if it’s okay to commit your files so they can go to GitHub. Once you say yes, it will ask you to restart your RStudio window to bring in the Git pane, to upload your files. Once your RStudio is restarted use the “Diff” button to commit your changed files (if any). Now, use the usethis::use_github to send your files to a GitHub repository. - use_github will ask if you have ssh keys set up, you probably will not so select “https.” It will then ask if your title and description are acceptable, if they are, you can say yes and upload it to GitHub!

100.4 Live Demonstration

  • A video explaining the basics of GitHub in RStudio:

100.5 Test Your Understanding

100.6 Common Mistakes and Errors

The most common error that will occur when working with GitHub in R are conflicts. Conflicts occur when you attempt to make a change on a file and when you upload the file to GitHub it doesn’t match its records. The previous module called “Dealing with Conflicts” explains what to do when these errors occur.

100.7 Next Steps

For more information about using GitHub in RStudio, check out the following links:

100.8 Questions

  1. True or False, git must be downloaded on your computer for GitHub functions to work in RStudio?
  1. True
  2. False
  1. Which button is not included in the Git pane in RStudio?
  1. Diff
  2. Push
  3. Pull
  4. Resolve Conflict
  1. What does the “Diff” button do?
  1. Pulls and Pushes your files to the repo
  2. Opens up the repo on a web browser
  3. Shows you the files you have changed and what has been changed
  4. Shows all of the files in your R project
  1. What do the push and pull buttons do?
  1. Upload and Download changes to/from GitHub
  2. Show your file edits
  3. Create a new branch for you to work on
  4. Checks your files for issues
  1. True or False, once you have created an R project, you cannot upload it to GitHub?
  1. True
  2. False
  1. Which package contains functions that let you work with GitHub in R?
  1. gitr
  2. usethis
  3. rgit
  4. tidyverse
  1. Which function allows you to use GitHub on an R project?
  1. use_git
  2. use_github
  3. to_git
  4. to_github
  1. What parts of RStudio let you upload files to GitHub? (select all that apply)
  1. Console
  2. Terminal
  3. Git Pane
  4. Files Pane
  1. What should be placed before commands if you want to use the terminal for git?
  1. "git _"
  2. "togit _"
  3. "github _"
  4. "upload/download _"
  1. True or False, files need to be committed before they are uploaded to GitHub?
  1. True
  2. False