World map

 

BM: Baca dalam Bahasa Melayu

World 10: Deep Git

Objects and hashes

You will learn: what Git really stores, and how each piece gets its name.

The problem

Your shop has 3 commits. style.css never changed. Does Git store it 3 times?

Pick one answer.

Watch

git cat-file -p HEAD prints the newest commit: a tree id, a parent id, the author, the committer and the message Add opening hours. Terminal output: $ git cat-file -p HEAD tree f531e32baf337b201dd21faa7c677933cd9421ea parent 1a02b3e07c19a715c61978990bddc1d32468eaa7 author You <you@example.com> 1793322240 +0800 committer You <you@example.com> 1793322240 +0800 Add opening hours

Git keeps everything as object: One thing Git stores: a blob, a tree, a commit or a tag. Its name is a hash of its content.. git cat-file -p HEAD opens the newest commit. It points to a tree: An object that lists one folder: names that point to blobs (files) and trees (folders). (the files). Its parent is the commit before.

Try it

A blob: An object that holds the content of one file. No name, just the content. is the content of one file. Open the commit, then its tree, then css. Find the blob of css/style.css.

Find the blob that holds css/style.css.

Pick a commit to look inside.

Check

Three kinds of object. Which one holds each thing?

Drag a card into a box. Or tap the card, then tap the box.

        Watch

        git rev-parse HEAD prints the full 40-character id 9e8ddf8fb3a185e7920bc21542abe85b09f07e21. git log --oneline -1 shows the short id 9e8ddf8 and Add opening hours. Terminal output: $ git rev-parse HEAD 9e8ddf8fb3a185e7920bc21542abe85b09f07e21 $ git log --oneline -1 9e8ddf8 (HEAD -> main) Add opening hours

        Each object’s name is a SHA-1: The recipe Git uses today to make a 40-character id from content. hash (commit id): A unique code that names one commit, like a1b2c3d.. It has 40 characters, made from the content. The short id is just the start.

        Predict

        You copy about.html to about-copy.html and commit. How many new blobs does Git store?

        Pick one answer.

        Try it

        The fourth kind is a tag object: a label with a note. Open v1.0 and see where it points.

        c02eb601a02b3e9e8ddf8

        You did it!

        You can now open Git’s objects and name all four kinds.

        Go deeper (optional)

        Annotated tags (git tag -a) are objects. Lightweight tags are only refs. Git compresses each object and keeps it in .git/objects. git cat-file -t prints an object’s type. git cat-file -s prints its size. Git 3.0 plans SHA-256 ids for new repos.

        cat-file -t prints commit for HEAD, tree for HEAD's tree, and tag for v1.0. Terminal output: $ git cat-file -t HEAD commit $ git cat-file -t HEAD^{tree} tree $ git cat-file -t v1.0 tag