Snapshots with names
Git saves snapshots of your whole project and names each one by its contents. Make your first commit and see the name it gets.
Every project you’ll ever work on changes. Files get edited, deleted, renamed, broken and fixed again. Git is the tool almost every software team uses to remember all of it. It can tell you what the project looked like last Tuesday, who changed this line, and give you back the version from before you broke it.
The idea underneath is simpler than its reputation:
- You work on files as normal, in your working tree.
- When you have something worth keeping, you put those changes in the staging area (
git add). - Then you take a snapshot of everything staged (
git commit). The snapshot is permanent, and it gets a name.
That name is the interesting part. Git doesn’t number snapshots 1, 2, 3. It runs the snapshot’s exact contents through a hash function called SHA-1 and uses the 40-character result as the name. Same contents, same name, on every computer in the world. Change one letter and you get a completely different name.
Your first commit
The playground below is a small git that runs entirely in your browser. It isn’t a recording: it stores files exactly the way real git does. Every hash you see is the hash real git would print for the same file, author and time. Follow the mission, and use hint if you get stuck.
Two files in different folders contain exactly the same text. How many copies of that text does git store?
Git names contents by their bytes alone, never by the file name, so identical contents get the identical hash and are stored once. The names live elsewhere, as you'll see when you look inside .git.
Why staging exists
Beginners often ask why you can’t just commit straight away. Staging lets you choose what goes into a snapshot. Say you fixed a bug and also left some debugging junk lying around: you stage the fix, commit it, and the junk stays out of history. One commit should be one idea.
git status shows a file under 'Untracked files'. What does that mean?
Untracked means it exists in your working tree and has never been added. Git knows nothing about its history because it has none yet. git add starts tracking it.
Everything above ran in your browser. Now do it on a real cluster withthe tilt platform. You need Docker, kind and Tilt, about 15 GB of free disk and 8 GB of spare RAM. Setup guide · Can my machine run it?
On your own machine, with git installed:
mkdir panda && cd panda
git init
echo "hello, panda" > hello.txt
git add hello.txt
git commit -m "my first commit"
git log
git hash-object hello.txtgit hash-object hello.txt prints the same hash the playground showed for the same contents. Try it.
Sources
Missions and quizzes above earn their own XP. This marks the lesson done.