Configure Git
To ensure minimal challenges using Git during the class, we want to configure Git now with some default settings: identify yourself and set up your cache credentials. You only have to do this once per machine.
If you are configuring Git on your own computer, before doing anything else run the following code in the R console to ensure you have the required packages installed (you do not need to do it if you use R Workbench):
install.packages(c("usethis", "gitcreds", "gh"))
Follow the step-by-step written instructions in the tutorial below to set up Git. For a walkthrough of the setup process check out this video demonstration
Identify yourself
To track changes and attribute them to the correct user, we need to tell Git your name and email address. Go to the Console tab in R and run the code below by replacing Sabrina Nardin
and email@gmail.com
with your name and email address:
- Your name could be your GitHub username, or your actual first and last name
- Your email address must be the email address associated with your GitHub account
library(usethis)
usethis::use_git_config(user.name = "Sabrina Nardin", user.email = "email@gmail.com")
To check that Git got your credentials, go to Tools > Shell and run the following command in the shell/terminal tab that opens up:
git config --global --list
Cache credentials
In order to push changes to GitHub, you need to authenticate yourself. That is, you need to prove you are the owner of your GitHub account. When you log in to GitHub.com from your browser, you provide your username and password to prove your identity. But when you want to push and pull from your computer, you cannot use this method. Instead, you will prove your identity using one of two methods:
- Cache credentials for SSH (use this if you are using R Workbench)
- Cache credentials for HTTPS
1. Cache credentials for SSH
The Secure Shell Protocol (SSH) is a method for authenticating your identity when communicating with GitHub. While a password can eventually be cracked with a brute force attack, SSH keys are nearly impossible to decipher by brute force alone. Generating a key pair provides you with two long strings of characters: a public and a private key. You can place the public key on any server (like GitHub), and then unlock it by connecting to it with a client that already has the private key (your computer or RStudio Serve). When the two match up, the system unlocks without the need for a password.
On GitHub, the URL for SSH remotes looks like git@github.com:<OWNER>/<REPO>.git
. Make sure you use this URL to create a project or clone a repository. If you accidentally use the HTTPS version, the operation will not work.
To create and store an SSH key pair, run the following code in the R Console:
credentials::ssh_setup_github()
- It should say “No SSH key found. Generate one now?” Tell the computer yes
- You will see a long string of characters in the Console (that’s your key!). You should be asked to open a browser now, tell the computer yes. Then go back to your Console: copy and paste the SSH key (the whole line of text, including the first words that should say “ssh-rsa”) into the new browser window on GitHub. If you do not have a GitHub account, please register a free GitHub account before proceeding (see Lecture 1)
- Under “Title” give the key an informative title, something like
css-rstudio-server
orcss-rworkbench
to record the class and computer. Leave “Key type” as “Authentication Key” - Click the green button “Add SSH key”. If you are prompted to clear the GitHub security procedures (e.g. authentication code, GitHub password, etc.) do so.
- You should now see your key saved on GitHub and you are done!
2. Cache credentials for HTTPS
With this authentication method you will clone GitHub repositories using a regular HTTPS url like https://github.com/<OWNER>/<REPO>.git
. You will need a personal access token (PAT) and use that as your credential for HTTPS operations.
To get a PAT, run this code from your R console:
usethis::create_github_token(
scopes = c("repo", "user", "gist", "workflow"),
description = "RStudio Workbench"
)
This is a helper function that takes you to the web form to create a PAT.
- Give the PAT a description (e.g. “PAT for Computing for Information Science”)
- Change the Expiration to 90 days. This ensures the PAT remains valid through the end of the course. You can also set the token to never expire, but GitHub will warn you this is not as secure as an expiring token.
- Leave the remaining options on the pre-filled form selected and click “Generate token”. As the page says, you must store this token somewhere, because you’ll never be able to see it again, once you leave that page or close the window. For now, you can copy it to your clipboard (we will save it in the next step).
If you lose or forget your PAT, just generate a new one.
In order to store your PAT so you don’t have to reenter it every time you interact with Git, we need to run the following code:
gitcreds::gitcreds_set()
When prompted, paste your PAT into the console and press return. Your credential should now be saved on your computer.
To confirm your PAT is saved, run the following code:
gh::gh_whoami()
usethis::git_sitrep()
You should see output that provides information about your GitHub account.
Now that you have stored your PAT, you should not be asked to provide a username and password when you attempt to push to or pull from GitHub. It will just work! Hopefully.
Acknowledgments
This page is derived in part from “UBC STAT 545A and 547M”, licensed under the CC BY-NC 3.0 Creative Commons License.
This page has been developed starting from Benjamin Soltoff’s “Computing for the Social Sciences” course materials, licensed under the CC BY-NC 4.0 Creative Commons License.