Version Control With git

Session 3

September 12, 2023

Review

What are your questions from last session?

Review

What stood out to you when (if) you diagrammed your file system?

Review

If your current working directory is Users/kristinariemer/Dropbox/Documents, what command could be used to change directory into the local Documents folder?

Learning objectives

  • Understand the what and why of version control
  • Set up git on your computer
  • Create a git repository associated with an R project
  • Use git to track and record changes to a git repository
  • Use .gitignore to selectively exclude files from git.

Version Control

Semi-automated system for tracking changes with unlimited “undo”

Why Learn Version Control?

  • Track development of research projects like a lab notebook

  • Keep project in sync across computers including HPC

  • Share and collaborate on projects in a clear and manageable way

Git and GitHub for version control

  • Git: version control on your computer.

  • GitHub: connects your local Git to the cloud.

    • Project management

    • Collaboration

    • Visual interface

    • Optional!

Coding time: Git Setup

We installed git last week, so you should be ready to set it up!

Shell
git config --global user.name "Your Name"
git config --global user.email "youremail@arizona.edu" 
# Use your GitHub associated email

Coding time

  • General syntax: git verb options
  • Set up text editor choice:
git config --global core.editor "vim" 
  • Use the --help flag to see options

Git repositories

  • Git works with the unit of the repository
  • Repositories are folders
  • Good practice: repositories == .Rprojects

Creating a git repository

You have options:

  • RStudio Wizard when creating an R project
  • git init in an existing directory/R Project
  • usethis::use_git in an existing R Project

RStudio New Project Wizard

  • Before you create the directory…
  • File > New R Project…

RStudio New Project Wizard

git init

  • Create a new directory (or R project) on your computer

git init

git init

  • Navigate to it on the command line
  • Run git init from the command line

git init

usethis::use_git()

  • Create a new R Project

usethis::use_git()

usethis::use_git()

  • Open the R Project
  • From inside the R project, run…

usethis::use_git()

Coding time

  • Create a new R project and initialize it as a git repo.
  • Use any of the 3 approaches we’ve just gone over.
  • Report back: how did it go?

Coding time

  • Introduce git status

Coding time

  • Introduce modifications to this repository
  • Try git status again

Saving modifications: a two-step process

Coding time

  • git add
  • git commit --m "foo"

Adding multiple files to same commit

Coding time

  • Modify-add-commit cycle
  • git diff
  • git restore to reset saved changes to the last commit
  • git reset HEAD\~ to (effectively) undo an add-commit cycle

Coding time

  • Ignore files with .gitignore

Takeaways

  • git handles keeping track of versions and changes
  • Initialize a git repo 3 ways, depending on the current state of your project
  • The core git workflow is the modify-add-commit cycle
  • git can travel backwards in time
  • Ignore files using .gitignore

Homework

  • Create a GitHub account if you don’t have one.
  • Slack us your GitHub username!
  • Optional: Identify an existing folder on your computer and turn it into a git repository.

Resources

Resources: Git