Git & GitHub

Tools for robust, collaborative coding

Last Time

  1. dplyr verbs take a data frame as their first argument and return one.
    • slice()
    • select()
    • filter()
    • mutate()
    • arrange()
    • summarize()

Key Ideas for Today

  1. git tracks changes to files.
  2. A repository (repo) is a directory tracked by git.
  3. The staging area holds changes for a snapshot.
  4. A commit is a snapshot of staged changes with a message.
  5. A remote is a web-hosted repo (GitHub) you push to and clone from.

Computational Toolbox

  1. Unix Shell: Organize and edit file system.
  2. R: Computational enrivonment to make data products to write to file system.
  3. git: Track changes to files on files system.

git

  • Free and open-source command line tool
  • Developed to help coordinate collaborative coding
  • 99% of software projects use it
  • Many scientific projects use it

What does it do?

git allows you to
    save your work,
    document your process,
    share it and,
    collaborate with others.

Repository

A directory whose changes are tracked by git. Also called a “repo”. Initialized with git init.

Staging Area

The set of changes queued up to be included in the next snapshot. Files are moved into it with git add.

Commit

A snapshot of the files in the staging area, saved with a descriptive message. Created with git commit -m.

Git commands to get started

Each command is called from the command line following git (e.g. git init).

  • init: initialize a repository (repo)
  • add: move a file into the staging area
  • commit: take a snapshot of files in the staging area with a message (-m)
  • push: send commits to another repo

  • init: invite the photographer (git) to the wedding (repo)
  • add: move the bride and groom into the camera frame
  • commit: take a picture of the bride and groom and caption it
  • push: share the photos online

  • init: invite the photographer (git) to the wedding (repo)
  • add: move the bride and groom into the camera frame
  • commit: take a picture of the bride and groom and caption it
  • push: share the photos online
mkdir wedding
cd wedding
git init
git add bride groom
git commit -m "cutting the cake"
git push

Example 1: Demo Project

mkdir demo-project
  1. Create new directory for my project.

mkdir demo-project
cd demo-project
  1. Create new directory for my project.
  2. Move into that directory.

mkdir demo-project
cd demo-project
git init
  1. Create new directory for my project.
  2. Move into that directory.
  3. Initialize git repo.

mkdir demo-project
cd demo-project
git init
echo "# My First Project" > README.md
  1. Create new directory for my project.
  2. Move into that directory.
  3. Initialize git repo.
  4. Create file using echo cmd (not git).

mkdir demo-project
cd demo-project
git init
echo "# My First Project" > README.md
git add README.md
  1. Create new directory for my project.
  2. Move into that directory.
  3. Initialize git repo.
  4. Create file using echo cmd (not git).
  5. Add file to staging area.

mkdir demo-project
cd demo-project
git init
echo "# My First Project" > README.md
git add README.md
git commit -m "initial commit"
  1. Create new directory for my project.
  2. Move into that directory.
  3. Initialize git repo.
  4. Create file using echo cmd (not git).
  5. Add file to staging area.
  6. Commit the change with message.

mkdir demo-project
cd demo-project
git init
echo "# My First Project" > README.md
git add README.md
git commit -m "initial commit"
# edit file
  1. Create new directory for my project.
  2. Move into that directory.
  3. Initialize git repo.
  4. Create file using echo cmd (not git).
  5. Add file to staging area.
  6. Commit the change with message.
  7. Make changes to file.

mkdir demo-project
cd demo-project
git init
echo "# My First Project" > README.md
git add README.md
git commit -m "initial commit"
# edit file
git add README.md
git commit -m "Add project description"
  1. Create new directory for my project.
  2. Move into that directory.
  3. Initialize git repo.
  4. Create file using echo cmd (not git).
  5. Add file to staging area.
  6. Commit the change with message.
  7. Make changes to file.
  8. Add file to staging area.
  9. Commit the change with message.

mkdir demo-project
cd demo-project
git init
echo "# My First Project" > README.md
git add README.md
git commit -m "initial commit"
# edit file
git add README.md
git commit -m "Add project description"
git log --oneline
  1. Create new directory for my project.
  2. Move into that directory.
  3. Initialize git repo.
  4. Create file using echo cmd (not git).
  5. Add file to staging area.
  6. Commit the change with message.
  7. Make changes to file.
  8. Add file to staging area.
  9. Commit the change with message.
  10. View the commit history.

Guess the Metaphor

A tracking files on git is like wandering into Medusa’s Lair: first you walk into the lair (git add) then she petrifies you where you stand (git commit).

Terracotta Warrior of Xian

GitHub

What does it do?

git allows you to
    save your work,
    document your process,
    share it and,
    collaborate with others.

GitHub

GitHub

  1. Allows you to create web-hosted repos to share with others
  2. “Facebook for coders”
    • Individuals or projects have their own accounts/profiles
    • Can follow one another

Create a repo

Live coding to set up https://github.com/andrewpbray/demo-project.git

Sharing your work

Sharing your work

Remote

A web-hosted copy of a repository (e.g. on GitHub) that you link to with git remote add and sync with git push. The default name is origin.

  • remote add: link your camera to the online album
  • push: sync photos with online album
git remote add wedding-pics wedding-pics.com
git push -u wedding-pics main

git remote add origin https://github.com/andrewpbray/demo-project.git
  1. Add new remote repo called origin.

git remote add origin https://github.com/andrewpbray/demo-project.git
git push -u origin main
  1. Add new remote repo called origin.
  2. Push all commits in local repo to remote

Project Workflow

Survey

  1. Prompt you to set up GitHub account.
  2. Guide you through git installation.
  3. Guide you through git configuration.

Project Workflow

boardwork

Key Ideas for Today

  1. git tracks changes to files.
  2. A repository (repo) is a directory tracked by git.
  3. The staging area holds changes for a snapshot.
  4. A commit is a snapshot of staged changes with a message.
  5. A remote is a web-hosted repo (GitHub) you push to and clone from.

For Next Time

  1. Quiz 2: 9/21-9/22.
  2. Complete the git setup survey (GitHub account, install, configuration).
  3. Project 2 is nigh!