Before configuring Git/GitHub and RStudio, you must first have installed the necessary tools. Install R and RStudio, or log into RStudio Server or RStudio Cloud. You will also need to create an account with GitHub.1 You may want to choose a user id that you are comfortable sharing in a professional setting (e.g., job interview), and even better if it is recognizable as you. For example, Matthew Beckman is more easily recognized by a user id like
mdbeckman when compared to
Verify whether or not Git has been installed already. Even if you have never used Git before, it may already be available to you (e.g., RStudio Cloud users can safely skip this).
If you aren’t sure, try this:
$), enter these two commands one at a time (according to your operating system):
Mac OS & Linux users:
which git git --version
where git git --version
.../bin/git”3 e.g., Windows path often resembles:
C:/Program Files/git/bin/git.exe4 e.g., Mac OS path often resembles:
Next, you need to tell Git who you are. The
usethis package includes a few R functions that can help. Edit the following R commands (i.e., change the name and email address to your own) and run each one in the R console:
library(usethis) use_git_config(user.name = "Andrea Carlson", user.email = "email@example.com") git_vaccinate() git_sitrep()
To clarify a few details,
user.nameshould be your proper name, rather than a GitHub ID.
user.emailmust match one associated with your GitHub profile.
git_vaccinate()is optional, but recommended (see R help for details).
git_sitrep()function simply prints a “situation report” to the console so you can verify that Git properly recognizes your name and email address, among other details.
Step 1. Log into your GitHub profile on the Internet.
Step 2. Create Repo for your project and initialize it with a README document (or navigate to an existing Repo)
Step 3. Click the green button “Code” and then copy the URL as shown below
Now that we have identified a GitHub Repo, we are ready to configure RStudio to recognize it as a “project.”
Step 4. In RStudio Select “File >> New Project” OR Click on the “Project” menu shown in the upper right corner of the RStudio Window
Step 5. Select “Version Control”
Step 6. Select “Git”
Step 7. Establish the connection (Clone Git Repository)
You’re in! (hopefully)… Note that the “Project” menu shown in the upper right corner of RStudio now shows the name of a Repo/Project. Also, a new “Git” tab appears and has started tracking file changes in a Repo.
About 95% of the workflow can happen in RStudio for basic use in scope for this book.
Occasionally, you may want to exclude a file from version control (e.g., large files, *.Rproj)10 RStudio >> Git tab >> More >> “Ignore…” or even throw out changes to a specific file and reinstate the most recent committed version.11 click the target file to highlight it and then navigate to RStudio >> Git tab >> More >> “Revert” Both actions can be accomplished directly within the RStudio IDE.
If you want to prevent GitHub from requesting your username & password each time you Pull or Push, you need to cache your credentials.
Set and verify credentials. In RStudio, open a project connected to a GitHub repo and then
If prompted for credentials on the second “Pull,” carefully follow the instructions again. The instructions provided rely on a credential helper that depends on your operating system and version of Git installed. If the credential helper functionality wasn’t automatically triggered, consult a more complete tutorial12 e.g., https://happygitwithr.com/credential-caching.html) and carefully follow the instructions associated with your operating system.
If (when) you encounter problems you might look here first…