February 9, 2017

Welcome to R-Ladies RTP!

Materials

Git Background

Why version control?

PhD Comics

Why version control?

  • Simple formal system for tracking all changes to a project

  • Time machine for your projects
    • Track blame and/or praise
    • Remove the fear of breaking things
  • Learning curve is steep, but when you need it you REALLY need it



Your closest collaborator is you six months ago, but you don't reply to emails.

– Paul Wilson, UW-Madison

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
    • Works best with text
  • Branching
    • Smarter merges

Git Demo

GitHub account

https://github.com/

Username tips:

  • Incorporate actual name
  • Reuse your username from other contexts, e.g., Twitter or Slack
  • Be as unique as possible in as few characters as possible – useful for GitHub auto-complete
  • Make it timeless. Don’t highlight your current university, employer, or place of residence.
  • Avoid words laden with special meaning in programming

Public vs. private repos

  • Public repos are free
  • Free private repos for users and organizations in education, academic research, nonprofits, and charities
    • Otherwise private repos for $

R and RStudio

The shell

  • In RStudio, Tools -> Shell

  • This will take you to the shell in your current directory

Checkpoint #1: Is Git already installed?

Go to the shell. Enter which git to request the path to your Git executable:

which git
## /usr/bin/git

and git --version to see its version:

git --version
## git version 2.6.4 (Apple Git-63)

IF NOT: See http://happygitwithr.com/install-git.html or follow along with someone near you who has git installed.

Introduce yourself to Git

We want to let git know who we are so there are some simple configuration options we should setup.

Let's first tell git who we are, and what editor we want to use.

$ git config --global user.name "Mine Cetinkaya-Rundel"
$ git config --global user.email "mine@stat.duke.edu"
$ git config --global core.editor nano
$ git config --global --list
  • If using multiple machines, do this on every machine.
  • user.name does NOT have to be your GitHub username, although it can be. Another good option is your actual first name and last name. Your commits will be labelled with this name, so this should be informative to potential collaborators.
  • user.email must be the email that you used to sign up for GitHub.

Some initial configuration

Make sure to put this information in your github profile as well.

Git clients

Connect to GitHub

Make a new repo

  • Go to https://github.com and make sure you are logged in.

  • Click green "New repository" button. Or, if you are on your own profile page, click on "Repositories", then click the green “New” button.

  • Repository name: myfirstrepo (or whatever you wish, we will delete this)

  • Public

  • YES Initialize this repository with a README

  • Click big green button "Create repository"

  • Copy the HTTPS clone URL to your clipboard via the green "Clone or Download" button

Clone the repo

Making local changes, save, and commit

  • Edit the README.md file in RStudio
    • Remove something from an existing line
    • Add something to a new line
  • Go to the Git tab and view the Diff

  • Check box next to README.md to Stage: Makes git aware of the current version of both files, but we have not actually saved the changes yet.

  • Enter commit message
    • Concise, but informative is best
    • Ok if it's obvious, it will be easier to browse commit messages than diffs later
  • Hit Commit to save the changes (locally)

Push your changes to GitHub and view

  • And finally Push
    • You might be prompted for your GitHub username and password
  • Go to your GitHub repo and view your commit history

What's with the yellow question marks?

  • These files got created when you created the R project

  • .gitignore lists files that won't be tracked by git

  • *.Rproj is the R project information

EXERCISE:

  • Stage, commit, push both

  • Review your commit on GitHub

Already tired of entering your username and password?

EXERCISE: Add a new file

  • Create a new R Markdown file (just the example template will do)

  • Knit HTML

  • Stage, commit, push, review commit on GitHub

  • Want to see your results/graphs on GitHub? Use output: github_document instead.

Making the problem worse

What if at the same time a collaborator was making changes to the same file?

On GitHub:

  • Edit README.md
  • Commit, with a message

In RStudio:

  • Edit README.md again, changing something else on the same line
  • Stage, commit, push

github README status

Merging remote changes

  • Before you can push, you'll need to pull

  • And then resolve the merge conflict

  • Finally you can push

Git commands -> RStudio actions

  • Stage -> git add

  • Commit -> git commit

  • Push -> git push

  • Pull -> git pull

Acknowledgments

Acknowledgments