Version Control

2022 DSS Bootcamp

Colin Rundel

08-25-22

Preliminaries

GitHub account

To get everyone on the same page:

  • If you don’t have one, sign up for a GitHub account (it takes 1 min.)
    • Go to github.com/join
    • Enter your information (username, email address and a password).

Account names

A few suggestions on picking a GitHub username:

  • Incorporate your actual name! People like to know who they’re dealing with. It makes your username easier for people to guess or remember.

  • Pick a username you will be comfortable revealing to your future boss.

  • Shorter is better than longer.

  • Be as unique as possible in as few characters as possible. In some settings GitHub auto-completes or suggests usernames.

  • Make it timeless. Don’t highlight your current university, employer, or place of residence.

  • Avoid words laden with special meaning in programming.

Accessing RStudio Workbench

To get started as quickly as possible, the preferred method is to use DSS RStudio server(s).

To access RStudio Workbench:

  1. Navigate to
  2. Log-in with your Duke NetID and password.

If you are having trouble accessing RStudio Workbench see the next slide.

DSS RStudio alternatives

If you cannot access RStudio via the DSS servers:

  • Make sure you are not using a custom DNS server

    • e.g. 1.1.1.1 or 8.8.8.8
  • Use a Docker container from Duke OIT

    1. Go to cmgr.oit.duke.edu and login
    2. Select Reserve a Container and find RStudio - statistics application with Rmarkdown and knitr support
    3. Click the link under my reservations to create your environment
  • Install R and RStudio locally on your computer

    • Download R on your computer here
    • Download RStudio here

Bootcamp materials

git and GitHub

Why version control?

  • Simple formal system for tracking changes to a project over time

  • Time machine for your projects

    • Track blame and/or praise
    • Remove the fear of breaking things
  • Learning curve can be a bit steep, but when you need it you REALLY need it

Why git?

  • Distributed

    • Work online or offline
    • Collaborate with large groups
  • Popular and Successful

    • Active development
    • Shiny new tools and ecosystems
    • Fast
  • Tracks any type of file

  • Branching

    • Smarter merges

Verifying git exists

git is already set-up on the DSS server(s). In the terminal tab you can verify this by running:

[cr173@numeric1 ~]$ git --version
git version 2.20.1

 

[cr173@numeric1 ~]$ which git
/usr/bin/git

Git sitrep

Within R we can also use the usethis package to get a helpful summary of the current git configuration,

usethis::git_sitrep()
## Git config (global)
## ● Name: <unset>
## ● Email: <unset>
## ● Vaccinated: FALSE
## ℹ See `?git_vaccinate` to learn more
## ℹ Defaulting to https Git protocol
## ● Default Git protocol: 'https'
## GitHub
## ● Default GitHub host: 'https://github.com'
## ● Personal access token for 'https://github.com': <unset>
## x Call `gh_token_help()` for help configuring a token
## Git repo for current project
## ℹ No active usethis project

git and GitHub live demo

Configure git

The following will tell git who you are, and other common configuration tasks.

usethis::use_git_config(
  user.name = "Colin Rundel",
  user.email = "rundel@gmail.com",
  push.default = "simple"
)

You will need to do this configuration once on each machine in which you use git (e.g. the server and your laptop).

This can also be done via the terminal with,

$ git config --global user.name "Colin Rundel"
$ git config --global user.email "rundel@gmail.com"
$ git config --global push.default simple

Configure git verification

To verify you configured git correctly, run

usethis::git_sitrep()
## Git config (global)
## ● Name: 'Colin Rundel'
## ● Email: 'rundel@gmail.com'
## ● Vaccinated: FALSE
## ℹ See `?git_vaccinate` to learn more
## ● Default Git protocol: 'https'
## GitHub
## ● Default GitHub host: 'https://github.com'
## ● Personal access token for 'https://github.com': <unset>
## x Call `gh_token_help()` for help configuring a token
## Git repo for current project
## ℹ No active usethis project

You should see output similar to above with your details.

Configure SSH and GitHub (authentication)

We will be authenticating with GitHub using SSH via public / private keys. We can create a new key pair (if necessary) by running the following in RStudio’s console:

credentials::ssh_setup_github()
## No SSH key found. Generate one now? 
## 
## 1: Yes
## 2: No
## 
## Selection: 1
## Generating new RSA keyspair at: /home/guest/.ssh/id_rsa
## Your public key:
## 
##  ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/wH7pT3UXdOMJSX2wMaPVTyGnYkS8OPmcfjct6h8Q+44/9UG3sOibjjUCxIxVeCWAoYFB0rDI3/Ljf2EWozLlpeGzAe7xsg6A+MHtUObZnfzXSB/NnOhZymD2u8Nh+py07aojVdKAPBkRH3nHA+rljidc3gXZkqseetYEI1N79OQUshp2P+Qm6Vab4I5OCnfAwLFkR7Sw7J9hvZN1qUmM0DB0WTWSlNmPSMsASMe/6Nz30IRoBh35Z7tgF79rlIW385giCkEeD20Le9EOueGoTWarJWylE1RWnUyig2mZ9JK/rYTw4KBXacPhBwn+MgGC+r8xY5IEX78xkeXW9q2z #<<
## 
## Please copy the line above to GitHub: https://github.com/settings/ssh/new
## Would you like to open a browser now? 
## 
## 1: Yes
## 2: No
## 
## Selection: 1

Getting started today

In order to get started, you need to obtain today’s files from GitHub. The steps below will give you access.

  1. Log in to GitHub

  2. Navigate to github.com/dukestatsci/computing_bootcamp_2022

  3. Fork the repository

  4. Copy the link under Clone or Download to clone with SSH.

  5. In RStudio go to File > New Project > Version Control > Git

  6. Paste the URL, that you copied in step 4, in the box under Repository URL

You now should have all the files in the repository in a directory on the server or your own computer.

Introduction to git

git Verbs (commands)

  • status - returns details about the current status of your git repository

  • add / rm - stage (or unstage) a local file with changes so that the changes will be saved when you commit.

  • restore - revert local changes that have not yet been committed

  • commit - save staged local changes

  • push - send local committed changes to a remote git instance

  • pull - collect committed changes from a remote git instance

Activity

We will now make sure that everything is working correctly by making changes to the README.md file in the repository.

  • Make a small change to README.md (i.e. add you name) and save the file.

  • Stage & commit that change using RStudio’s git GUI

  • Push the changes to GitHub

  • Check that the changes are reflected on GitHub

Version control best practices

  • Commit early, often, and with complete code.

  • Write clear and concise commit messages.

  • Test code before you commit.

  • Use branches.

  • Communicate with your team.

git and GitHub resources