This is a free preview. Enrol on the course to save your progress and open every lesson.

Lesson 1 of 28 · 15 min

Commits are snapshots: Git's objects

Look inside the .git folder

Unzip the starter files and open a terminal in the pay-calculator folder. It's a small project with 12 commits. Because of the way it was made, your commit IDs will be exactly the same as in these lessons — as long as you start each module from a freshly unzipped copy.

$ git log --oneline -3
40bd88b Add contributing guide
430212b Add note about award rates
46895e5 Reject negative hours

Everything is an object

Git stores four kinds of objects, each named by a hash of its contents:

ObjectHolds
blobThe contents of one file
treeA folder: names pointing to blobs and other trees
commitA tree (the whole snapshot), its parent commit(s), author and message
tagA named, annotated pointer to a commit

You can look at any object with git cat-file:

$ git cat-file -t HEAD
commit
$ git cat-file -p HEAD
tree 03ea1ebf708461b566f7fa4a889f3fc3edcf953c
parent 430212b1c41844b5d3f2b0f464b9ed8d969c6e2b
author Sam Lee <12345678+sam-lee@users.noreply.github.com> 1770937200 +1100
committer Sam Lee <12345678+sam-lee@users.noreply.github.com> 1770937200 +1100

Add contributing guide
$ git cat-file -p HEAD^{tree}
100644 blob c61f6af07f717d2e10c496e722bbe78a1d886866	.gitignore
100644 blob 2151d22eef6d7529c1d7717bde24c601b2fa51e6	CONTRIBUTING.md
100644 blob c59bfab7d1de7eed9f157935a413d901ce5d2068	README.md
100644 blob fd9ded9f0ccc570fcd740d35e584041cc1dee038	pay.py
100644 blob b7e429c85257395dc96c4c80b06be300c672bf5f	test_pay.py
$ git cat-file -p HEAD:README.md
# Pay calculator

Works out pay for a shift, including weekend rates.

Example rates only — real rates depend on the award.

What this tells you

  • A commit is a complete snapshot (a tree), not a list of changes. Diffs are worked out when you ask for them
  • Each commit points to its parent, so the history is a chain you can walk back
  • Change anything — even one letter of a message — and the hash changes. That's why Git history is so hard to tamper with
  • Files that don't change between commits are stored once and shared, which keeps repos small

Try it

Run git cat-file -p HEAD~1. Is its tree the same as HEAD's? (The README didn't change in the last commit — can you find which blob is shared?)

Downloads

2 files to download once you enrol.