Course outline · 0% complete

0/28 lessons0%

Course overview →

Repos, commits, and history

lesson 1-2 · ~10 min · 2/28

Three words you'll use every day

Git's whole world is built from three ideas:

  • A repository (or repo) is a folder that Git is watching. Git keeps its entire database inside a hidden subfolder called .git. Delete .git and the folder becomes an ordinary folder again.
  • A commit is a saved snapshot of the project at one moment, together with a message explaining it, the author's name, and the date. Think of it as a save point in a video game.
  • The history is the chain of all commits, oldest to newest. Each commit remembers which commit came right before it, called its parent.

That's it. Everything else in this course is commands for creating commits, looking at them, and moving between them.

123a1b2c3de4f5a6bc7d8e9fAdd recipeFix typoAdd photonewest commitparentparent
History is a chain. Every commit stores a full snapshot and points back to the commit that came before it, its parent.

Snapshots, not diffs

A common misconception: people imagine each commit stores only the lines that changed. Mentally, treat it the other way. Each commit is a complete snapshot of every tracked file at that moment. (Internally Git compresses cleverly, so this costs almost no disk space.)

Every commit gets a unique id, a 40-character code like c7d8e9f2a91b... called a hash, computed from the commit's contents. Since the first 7 characters are almost always unique within a project, Git lets you use the short form, like c7d8e9f. When you see a hash in this course, that's all it is: the name of one specific snapshot.

Why the id is computed, not counted

Git does not number commits 1, 2, 3. Each id is computed from the commit's contents — the files, message, author, date, and parent — using a hashing function: a function that turns any text into a fixed-length code. This rule has two consequences that make Git trustworthy:

  • The same contents always produce the same id, on any computer. Your machine and a teammate's machine agree on every commit's name without a central numbering authority — which is what will let separate copies of a repository cooperate in unit 7.
  • Changing anything, even one letter, produces a completely different id, so a commit cannot be quietly altered after the fact. If the contents changed, the name changed.

You can watch both rules in action below.

Code exercise · python

Run this. It uses sha1, the hashing function Git historically uses, on two versions of a snapshot. Hashing the same text twice gives the same id; changing one word gives a completely different id. ([:7] keeps the short 7-character form you see in git log.)

Quiz

What does a single Git commit contain?

Problem

Git stores its entire database inside one hidden subfolder of your project. What is that folder called? (You saw hidden folders and `ls -a` in the terminal course.)

Quiz

Commit 2 was made right after commit 1. Which statement is true about how they are linked?