Git

a gentle introduction

Quality coding #6

Dan Fitch

2020-12-02

What we'll cover

  • What is it?
  • Simple git workflows
  • The stage and how not to get confused
  • Different clients
  • Using git to do various stuff

What is it?

  • Distributed version control
  • What can it do?
    • You can easily track changes (by yourself or by multiple people)
    • Easy to save or share prototype work in branches
    • You can work offline
    • Easy-ish to collaborate using different merge workflows

Okay, but why would I use it?

  • Quick and easy reversability and history
  • Concurrency: if two or more people are working on the same thing at the same time
  • Annotation separate from code comments: instead of commenting out code, delete it and comment in the log why, git makes it easy to find again

Complicated?

YES

Git is very complicated, in total. You can do... anything.

NO

If you use a subset of Git, it can be quite simple and very useful.

So let's try to keep it simple today.

Simple git workflow

git init starts

git status shows what's new/changed

git add adds files

git rm removes them

(or you can rm and git add .)

git commit -m "Saved!"

Getting started

Introduction to some terms, with some basic starter steps

Repo

Secret stuff lives in .git/

Everything else is your "work tree", your normal files

Make a repo

mkdir git-test

cd git-test

git init

ls -al

Stage

What changes are pending? Sometimes called index, but "staging" is clearer

Stage some files

touch new-file

git status

git add .

git status

Clean and dirty

If you do git status and see nothing, you are clean.

Changed files, new files, deleted files... those all create a dirty tree.

The three key places

Working directory

Stage

HEAD (of current branch, usually main)

ADD

puts changes from your working directory into the stage

CHECKOUT

puts original copy from HEAD and overwrites your working copy

STATUS

red lines are in working directory only

green lines are in stage

COMMIT

puts stuff from stage into HEAD

Let's commit!

git commit -m "A helpful message!"

git log

Getting fancier

Now let's clone an example repository:

cd ..

(don't put git repos inside other git repos)

git clone https://github.com/uwmadison-chm/bioread

cd bioread

Branch

Different versions, simultaneously

Git stores a directed acyclic graph (DAG)

Show some branches

git branch

git checkout -b newbranch

Tag

Naming a particular version, "v1.0.0"

git tag v2.2.0

Hash

Every version (state) of the tree has a generated name

Looking at hashes

git log --oneline

git diff b4a2

git diff v2.1.0

git diff v2.0.0..v2.1.0

Remote

A remote is a copy of the repo that we can push and pull changes to and from

Pushing git workflow

(pull OR fetch from some central place, then rebase your work on it)

push to somewhere

(often Github or similar host)

Master vs. main

git branch -m master main

Clients

Level 1 Git Wizard Spells

  • Who made a change? git blame
  • Searching across time and space: git log -S
  • Simple ways to compare branches or versions
  • Branching and merging

Level 2 Git Wizard Spells

  • Cherry-picking hunks and commits
  • rebase
  • rebase -i: interactively rewriting history!
  • Config tricks: Fancy logs
  • More complex workflows: pull requests, etc.

THE END

(or, Q&A discussion time!)