Git

in the lab

and also Gitlab

Dan Fitch

2024-10-03

Overview πŸ—ΊοΈ

  • What is Git?
  • Simple workflows
    • command line
    • in VSCode
    • (there are other clients too)
  • Gitlab and ticket tracking

One thing:

When should you interrupt?

RIGHT AWAY! πŸ’‘

(when you have a question) 🀨

What the heck is Git? πŸ€”

Distributed version control system

...😦 ...okay...

What can it do?

  • Easily track changes (by yourself and/or others)
  • Save or share prototype work in branches
  • You can work offline
  • Easy-ish to collaborate

Things Git isn't

Github

Gitlab

Always easy to use

Why would I use it? πŸ™‹

  • Great for scripts and code
  • Quick and easy reversability and history
  • Annotation separate from code comments: instead of commenting out code, just delete it, then make a commit and comment in the log why you deleted it. Git makes it easy to go back in the history and get it back

Complicated?

YES 😫

You can do... way too much

See Julia Evans's How Git Works for a great distillation of the details

NO 😎

If you use only some features of Git, it can be simple and very useful

So let's dive in, trying to keep it simple.

Key concepts πŸ”‘

Working directory

Stage

HEAD (of current branch, usually main)

ADD

puts changes from your working directory into the stage; prepares to get something permanently saved to Git

STATUS

shows changes that are in working directory only

shows things added to stage (pending)

CHECKOUT

gets the copy from Git's memory and overwrites your working copy (if you want to undo something)

COMMIT

puts stuff from stage into Git's permanent memory, with a message

Quick demo at the command line πŸ–₯️

git init starts new, git clone grabs existing

git status shows what's new/changed

git add adds files

git rm removes them

git commit -m "Saved!" to make a commit

Quick demo in Visual Studio Code πŸ–ΌοΈ

Init

Status

Add

Commit

What if I change a file I've already added?

Archaeology time! βš’οΈ

Example with Cole's brainageR

git log

git tag

git branch

Looking at history

git log --oneline

(or use an alias, git log1)

Branches, tags, and commit hashes are all ways to refer to a point in time in Git history

Looking at changes

git diff 4597..cc31

git diff 1.0..2.1

That's too much info! How can we limit this to a specific path?

git diff 1.0..2.1 README.md

Connecting things πŸ”—

Remote

  • A remote is what Git calls a copy of the repo that we can push and pull changes to and from
  • Often hosted on Gitlab or Github or similar host, but could just be a network drive location
  • Gitlab and Github support https:// and git:// protocols

Simple workflow

  • pull OR fetch from some central place
  • add and commit with a helpful message
  • push your changes to a remote

Pull request workflow

  • Same steps as before, but at the end ask a central maintainer to "pull" your changes from your remote into theirs
  • Used by larger groups and open source projects
  • Replaces the old "email me a patch" workflow

Collisions 🫣

OH NOES

Out of date

  • when you push, git pulls automatically and says you're out of date
  • OH NO! someone changed something, but it's not the same lines of the file as you
  • Git will automatically merge and rebase
  • push just like usual, WHEW

Merge conflicts

  • OH NO! someone changed the same thing as you and they pushed first!
  • you have to manually resolve with a 3-way diff tool
  • This is often VERY CONFUSING.
  • resolve the conflict, push, yikes

BUT...

  • working solo? this will never happen
  • working centrally? (like on the study drive?) this will never happen
  • but! you still have the issue that you would have without Git of two people having a "save war" where last save wins

Level 1 Git Spells πŸͺ„

  • Config aliases
  • Branching and merging
  • Who made a change to a specific line, or when? git blame
  • Searching across time and space: git log -S, git log -Sp

Level 2 Git Spells πŸ§™

  • Cherry-picking hunks and commits
  • rebase
  • ...and rebase -i: interactively rewriting history!
  • Dark magic

Gitlab 🦊

THE END

(open discussion time!)