TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to How Git works inside

๐Ÿ“ธ How Git works inside: cheat sheet

๐ŸŽจ The analogyA photo album where every photo is labelled with its fingerprint and each page points back to the one before.
๐ŸŽ“ The regular explanationGit is a content-addressed store of immutable objects (blobs, trees, commits and tags) named by their hash, plus small movable refs such as branches and HEAD that point into the commit graph.
Everything gets a fingerprintStep 5 of 5
The boxce0key blob: hello: ce00a75key tree: README.md, src/: a751872key commit: tree a75, First page: 872โœฆโœฆโœฆโœฆโœฆ2345๐Ÿง‘โ€๐ŸŽจYou๐Ÿ”Fingerprint readerce0136โ€ฆ
  1. You take a photo of one thing, a note saying hello. The fingerprint reader works out its fingerprint from exactly what is in the picture.
  2. The photo goes into the box, filed under its fingerprint. It has no name on it, only the picture.
  3. A second thing looks exactly the same. Same picture, same fingerprint, so the box doesn't keep a second copy.
  4. Next comes a contents card. It lists each photo by fingerprint and says what each one is called. The card gets a fingerprint of its own.
  5. Last comes the album page itself. It points to the card, says who made it and when, and has a caption. It gets a fingerprint too.
Snapshots that share photosStep 4 of 4
Album pagesef9key commit 1: ef9054akey commit 2: 54a1234Photos in the boxce0key README.md v1: ce00c9fkey App.java v1: c9f1d41key App.java v2: d41234๐Ÿง‘โ€๐ŸŽจYou1 thing changed๐Ÿ—ƒ๏ธ Packed boxdelta inside
  1. The first page shows the whole project. Both photos go into the box, a note and a drawing.
  2. You change only the drawing. The new page still shows the whole project, but the note's photo is the same, so only the new drawing goes in the box.
  3. Want to know what changed? Lay page 1 and page 2 side by side and compare them. Nobody stored the change itself.
  4. The box gets crowded, so it's packed neatly. The second drawing is kept as the first one plus a small note about the difference.
Bookmarks, the ribbon and the trayStep 4 of 4
Album pagesa1fkey commit a1f: a1f0b7ckey commit b7c: b7c1c3dkey commit c3d, parent b7c: c3d234mainHEAD๐Ÿง‘โ€๐ŸŽจYoudetached๐Ÿงบ The tray
  1. The album has two pages. The bookmark called main sits on the newest one, and the HEAD ribbon is tied to that bookmark.
  2. You change the drawing and put its new photo on the tray. Only what's on the tray goes onto the next page.
  3. You add a page from the tray. It points back to the page before, and the main bookmark moves onto it, taking the ribbon along.
  4. You lay the ribbon straight on an old page to look around. It isn't tied to any bookmark now, so stick one on before adding pages here.
Fast-forward or a joining pageStep 4 of 4
from b7cfrom b7csecond parentShared pagesa1fkey commit a1f: a1f0b7ckey commit b7c: b7c1234Ben's line, then mainc3dkey commit c3d, parent b7c: c3d0m6akey merge commit on main, parents c3d and e5f: m6aโœฆโœฆโœฆโœฆโœฆ1234fixmainCleo's pagesd4ekey commit d4e, parent b7c: d4e0e5fkey commit e5f, parent d4e: e5f1234feature๐Ÿ‘ฉAna
  1. Ana's main bookmark is on page b7c. Ben and Cleo both started from that page and each added pages on their own line.
  2. Ben's turn. Main hasn't moved since he started, so Ana just slides her bookmark forward onto his newest page. No new page is made.
  3. Cleo's turn. Main has moved on and so has Cleo, so sliding isn't possible. Git compares both lines with the page they share, b7c.
  4. Git makes a new joining page that points back to two pages, Ben's c3d and Cleo's e5f. The main bookmark moves onto it.
Copied pages and the diaryStep 5 of 5
Ana's pagesa1fkey commit a1f: a1f0b7ckey commit b7c: b7c1e5fkey commit e5f: e5f2c9akey copy of c3d, parent e5f: c9a3d8bkey copy of d4e, parent c9a: d8b45mainfeatureHEAD๐Ÿ‘ฆBen๐Ÿ““ Ribbon's diaryentry expired
  1. Ben's two pages start from b7c, but Ana's main has moved on to e5f. Ben's ribbon is on his newest page, d4e.
  2. Ben redoes his pages on top of e5f. Each copy points back to a different page, so it gets a new fingerprint, c9a and d8b.
  3. His bookmark and the ribbon jump to the copies. The old pages have no bookmark left, but the diary writes down where they were.
  4. If the copies went wrong, Ben looks up the old fingerprint in the diary and can put his bookmark straight back on d4e.
  5. Weeks later the diary entry is too old and gets crossed out. Now nothing reaches the old pages, so the tidy-up throws them away.
Fetch, then pullStep 4 of 4
Your album ยท fetch + joina1fkey commit a1f: a1f0b7ckey commit b7c: b7c1c3dkey commit c3d: c3d234mainorigin/mainThe friend's copya1fkey commit a1f: a1f0b7ckey commit b7c: b7c1c3dkey commit c3d: c3d234main๐Ÿ‘งTeammate
  1. Your album and the friend's copy both end at b7c. Your pencil bookmark, origin/main, says the friend's main was on b7c too.
  2. A teammate adds page c3d at the friend's house. Your pencil bookmark doesn't know, because you haven't visited since.
  3. Fetching means visiting, copying the new page into your box and moving your pencil bookmark. Your own main bookmark stays put.
  4. Pulling is fetching and then joining. Here main hasn't moved, so your bookmark simply slides forward onto c3d.

๐Ÿงฉ Key questions

You change one drawing in a big project and add a new page. What new things go into the box?
The new drawing's photo, new contents cards for it, and the page itself. Every page shows the whole project, but unchanged photos keep the same fingerprint, so the box keeps just one copy.
Ben redid his pages on top of Ana's and now wishes he hadn't. His bookmark only shows the copies. Can he get the old pages back?
Yes, the ribbon's diary still has the old fingerprint, so he can put his bookmark back on it. The old pages stay in the box until nothing, not even a diary entry, reaches them, and the tidy-up only throws out old ones.
You visit the friend's house and copy the new pages, but you don't join them in. What moved in your album?
Only your pencil bookmarks, like origin/main, and the new pages in the box. Fetching copies pages and updates the pencil bookmarks. Your own bookmarks only move when you join the new pages in, which is what pulling adds.
You lay the ribbon straight on an old page, add two new pages there, then tie the ribbon back to main. What happens to the two new pages?
They stay in the box, but no bookmark points at them; only the diary remembers where they are. No bookmark was on those pages, so they are hard to find. Stick a bookmark on first, or look them up in the diary soon.