Version Control with Git

David Wilby, Harrison Abbot, Kinzie Orton, James Byrne
British Antarctic Survey

Tue 9th April 2024

Welcome!

Schedule

9.30-11.00: Intro and setup (David)

11.00-11.15: break β˜•

11.15-12.30: Live coding exercises (Harrison)

12.30-13.30: lunch πŸ₯ͺ

13.30-15:00: Live coding exercises (Kinzie)

15.00-15.15: break β˜•

15.15-16.30: Final exercises & discussion (James)

Format

  • A little talking
  • Mostly live coding & practical exercises

Course material: https://carpentries-incubator.github.io/git-novice-branch-pr/

Get help!

β€œQuick” or urgent questions

In-person: βœ‹ in real life

Remote: βœ‹ on zoom


Bigger questions

Please wait until the end of a section

Icebreaker

  • What’s your name?
  • Introduce yourself
  • Why are you here today?
  • Who’s your favourite cartoon/book/film character?

  • David πŸ‘‹
  • Research Software Engineer (previously physics/sensory biology)
  • I ❀️ teaching git! πŸ€“

πŸ›‘ git setup

https://carpentries-incubator.github.io/git-novice-branch-pr/02-setup/

What is version control?


  • πŸ“Έ Snapshot current version
  • 🏷️ Name specific versions
  • β†©οΈŽοΈ Revert back to a particular version


Perhaps

  • πŸ“š Compare and merge versions

Benefits of version control

local πŸ’»

  • Protect against breaking everything
  • Keep at least one working version of the code
  • Snapshot your progress

remote/distributed 🌐

  • Work collaboratively
  • Share code easily
  • Remote backup

Without version control


πŸ˜• Make changes by making a copy of the entire codebase.


😐 Merging is a manual process.


😨 Lose track of which version contains what functionality.


😭 Collaborating is just emailing zip files and crying.


Version Control == Git

More often than not

How does Git work?

The most important concept in git is the commit - the name given to a unit of changes, and also to the process of making a commit.


Commits contain changes

Not actually snapshots of a file.

But can recreate a state from a sequence of changes.

Demo

https://onlywei.github.io/explain-git-with-d3

or

https://bit.ly/git-sandbox

or

Repositories (Repos)


Once a directory/folder is initialised with git it becomes a repository.



directory

.
β”œβ”€β”€ src/
β”œβ”€β”€ LICENSE.md
└── README.md

git init β€”β€”->

repository

.
β”œβ”€β”€ .git/
β”œβ”€β”€ src/
β”œβ”€β”€ LICENSE.md
└── README.md

Making a commit

The commit hash


Git generates a hash string, uniquely identifying each commit.

Git uses a β€œMerkle tree” under the hood. (Don’t ask me how it works, I have no idea 🀷)


Hashes look like:

d3dd03f493707256c8528bc83ad280a460f05a56


But are most often seen as the first 7 characters, as this is easier to read/type and is normally enough to identify the commit.

d3dd03f

The commit message

Each commit has a message associated with it.


Summary/Title: <50-72 characters

Displayed most frequently.


Detailed description: no character limit.

Can be used to capture more detail. Not used that often.

This commit will…

  • ❌ some stuff
  • ❌ code
  • ❌ updates
  • βœ”οΈ add new module β€œrenderers”
  • βœ”οΈ update README with new install instructions
  • βœ”οΈ fix bug #17 with package update

Branches

Used to work on new features/changes/additions to the code.

git branch experiment
git checkout experiment
git commit
git commit
git commit

Checkout: switching to a different branch.

Merging

Combine changes from two branches.

git checkout main
git merge experiment
git commit

How to interact with Git

command line git

via unix shell (or gitbash/WSL on Windows)

$ git add README.md
$ git commit -m 'initial commit'
$ git status


Git learning resources


Remember

Learning Git is a process.

Everyone makes mistakes.

Git vs GitHub or GitLab


Git

  • Local client for source code management
  • Interacting with remote git servers

GitHub/GitLab

  • Code hosting
  • Collaboration
  • OSS contribution
  • Project management
  • Automated workflows/continuous delivery

Repositories

Issues

Projects/Kanban Board

Continuous integration/Automated testing

Great resources