Learning Objectives
- Set global options in your
.gitconfig
file - Practice how to set up GitHub Authentication using a Personal Access Token (PAT)
2.1 Set up global options in Git
Before using Git, you need to tell it who you are, also known as setting the global options. To do this, we will be setting the global options in the Terminal.
To get started, let’s open a new Terminal window in RStudio. Do this by clicking Tools > Terminal > New Terminal.
A Terminal tab should now be open where your Console usually is.
Let’s start by adding your user name to the global options. Type the following into the command prompt, with your exact GitHub username, and press enter:
git config --global user.name "my_user_name"
Note that if the code ran successfully, it will look like nothing happened. We will check at the end to make sure it worked.
Next, enter the following line, with the email address you used when you created your account on github.com:
git config --global user.email "my_email@nceas.ucsb.edu"
When you add your username and email to the global options you must use the exact same spelling and case that you used on GitHub otherwise, Git won’t be able to sync to your account.
Next, we will set our credentials to not time out for a very long time. This is related to how our server operating system handles credentials - not doing this will make your Personal Access Token (PAT, which we will set up in the next section) expire immediately on the system, even though it is actually valid for at least a month.
git config --global credential.helper 'cache --timeout=10000000'
Next, we will set the default branch name to main
for any new repositories that are created moving forward. Why are we doing this? Previously, the default branch name was master
and this racist terminology for Git branches motivates us to update our default branch to main
instead.
git config --global init.defaultBranch main
Finally, check to make sure everything looks correct by entering this command, which will return the global options you have set.
git config --global --list
2.2 GitHub Authentication
GitHub recently deprecated password authentication for accessing repositories, so we need to set up a secure way to authenticate.
The book Happy Git and GitHub for the useR has a wealth of information related to working with Git in R, and these instructions are based off of Chapter 9 Personal access token for HTTPS.
We will be using a Personal Access Token (PAT) in this course. For better security and long term use, we recommend taking the extra steps to set up SSH keys (check out Chapter 10 Set up Keys for SSH).
Congrats! Now you’ve setup your authentication you should be able to work with GitHub in RStudio now.