That is an excellent technical question! While it seems like magic, Git's "Time Machine" is actually a very clever content-addressable filesystem.
Here is a simplified look at how the .git folder stores your history:
1. The Objects Database
Inside .git/objects, Git stores every version of your files. Instead of saving the differences (like some other tools), Git takes a snapshot of the file.
- Blobs: When you save a file, Git compresses the content and calls it a "blob" (Binary Large Object).
- Unique IDs (Hashes): Git gives every single version of a file a unique name (like
a1b2c3d...) using a mathematical formula called SHA-1. If you change even one comma in your file, it gets a brand-new unique ID.
2. Snapshots (Trees)
Think of a Tree object as a "snapshot" of your folder. It records which "blobs" (files) belonged together at a specific point in time, along with their filenames.
3. Commits (The Timeline)
A Commit is like a page in your project's diary. It points to a specific Tree (the state of the folder) and includes:
- Who made the change (Author).
- When it happened (Timestamp).
- Why it happened (The commit message).
- The Parent: Most importantly, it points to the previous commit. This "link" is what creates your project's timeline or history.
4. References (The Markers)
Inside .git/refs, Git keeps small text files called Refs.
- The
masterormainbranch is just a simple file that stores the ID of the latest commit. - When you make a new commit, Git moves that marker forward to point to the newest one.
In summary: When you ask Git to "travel back in time," it looks at the Commit ID you want, finds the corresponding Tree (snapshot), and then pulls the correct Blobs (files) out of the database to reconstruct your project exactly how it was!
You'll see these "Commits" in action in the upcoming steps of this lab! Ready to create your first snapshot?