What is Git and Why Use it?

Alice Bartlett of the Financial Times gave a great talk on this in 2016. She briefly shows and discusses the terrible user interface (UI). You can ignore those parts. We’ll talk about GitHub desktop that will make it much easier to work with Git. Slides from her talk are here.

To sum up, for our purposes Git is great because:

Setting up Git, GitHub, and Making them Talk with R Studio

Git

Hopefully now I’ve convinced you that using GitHub is a good thing, because setting them up the first time can been a little bit challenging. But the good news is you should only have to do this one time per computer. We’ll walk through how to do it on a Windows computer, but if you’re using a different operating system check out this resource on how to set up Git.

  1. Download git for windows: https://gitforwindows.org/

  2. Run the executable (.exe) file for installing git, you can leave all the default settings the way they are.

  3. Next we’ll double check that git was successfully installed:

  1. Open up RStudio, in the bottom panel you’ll see by default the console, but next to that you can select the terminal. Click terminal to switch to it.

  2. Here type git -- version, if you’ve got git installed it will tell you what version you have installed. If not, try to install again or ask Katie for help.

  1. Now we need to configure git, to do that, run the following lines in RStudio after updating your name and email. Nothing will happen, but behind the scenes this code will update your git files.
#install.package("usethis") #only run once to install package
library(usethis)

#tell git your name and email, should be email associated with your github account 
usethis::use_git_config(user.name = "Your Name Here",
                user.email= "Your Email Here")

GitHub

Now we’ll get GitHub set up, this is the online, remote which will store all your data online.

  1. Next you’ll need to sign up for a (free) GitHub account (https://github.com/).
    1. After you’ve done this, send your username to Katie () so I can add you to the WWS GitHub organization.
  2. Now we’ll link your GitHub account to RStudio. To do this you need to get an access token from GitHub. This is a long string of characters (basically a password) that connects RStudio to GitHub.

If you’re unsure if you’ve done this before, run the following lines of code if your username comes up, you’ve already got GitHub talking with RStudio and you can skip to step 7:

#install.packages(gitcreds) #run the first time 
library(gitcreds)
gitcreds::gitcreds_get()
## <gitcreds>
##   protocol: https
##   host    : github.com
##   username: katiewampler
##   password: <-- hidden -->

If you’ve never done this before run the following lines of code. This code will open up a GitHub window, you can leave the defaults as is. Just scroll to the bottom and hit the green “Generate token” button.

#install.packages(gitcreds) #run the first time 
library(gitcreds)

usethis::create_github_token()

Now we’ll use the gitcreds package to securely store your token, because once it’s generated you can’t get it from GitHub again. When it asks for password or token, paste the token you just got from GitHub.

gitcreds::gitcreds_set() 

GitHub Desktop

The last thing we need to install is GitHub desktop. While we can interface with GitHub from RStudio, GitHub desktop is even more user friendly.

  1. Download the installer and run to install (https://desktop.github.com/download/).

  2. Once it’s installed, open it and go to file -> options -> accounts. Click the button to sign into GitHub online and follow the prompts.

That’s it, now you’re ready to enjoy effortless version control and data backups!

Sources and Further Reading