From the free course The Missing Semester of Your CS Education on Lykke

Version Control and Git

Key concepts: Directed Acyclic Graph (DAG) · Blobs, Trees, and Commits · SHA-1 Content-addressing · HEAD and References

Understanding Git's underlying data model to master collaboration and project history.

Version Control and Git

Version Control Systems (VCS) are the backbone of modern software engineering. While many introductory courses focus on the syntax of specific commands, this chapter approaches Git from first principles. By understanding Git’s underlying data model—how it stores information and represents history—the often-confusing command-line interface becomes a logical set of operations on a graph.

1. The Philosophy of Version Control

In a traditional "Missing Semester" context, version control is the solution to the chaos of manual file management (e.g., final_v1.py, final_v2_fixed.py). A VCS allows you to:

  • Revert: Return files or the entire project to a previous state.
  • Track: See who made what changes and why.
  • Collaborate: Work on the same codebase with others without overwriting their work.
  • Experiment: Create isolated environments (branches) to test features without breaking the main project.

2. The Git Data Model

Most people struggle with Git because they treat it as a "black box" of commands. Instead, think of Git as a simple toolkit for managing a Directed Acyclic Graph (DAG) of snapshots. Git’s data model consists of three primary objects: Blobs, Trees, and Commits.

Blobs (Binary Large Objects)

A Blob is just a bunch of bytes. In Git, a blob represents the contents of a file. Crucially, blobs do not store the file's name or its permissions—only the data itself.

Trees

A Tree represents a directory. It maps names to either blobs (files) or other trees (subdirectories). A tree defines the structure of your project at a specific point in time.

Commits

A Commit is a snapshot of the top-level tree, combined with metadata. This metadata includes:

  • Author/Committer: Who wrote the code.
  • Message: Why the change was made.
  • Parents: A list of commits that came directly before this one. Most commits have one parent; merge commits have two or more; the initial commit has zero.

3. Content-Addressable Storage

Git identifies every object (blob, tree, or commit) by a SHA-1 hash. This is a 40-character hexadecimal string generated from the contents of the object.

  • Immutability: Because the ID is a hash of the content, you cannot change a file without changing its hash. This ensures data integrity.
  • Deduplication: If two files have the exact same content, Git stores only one blob and points to it from multiple trees. This makes Git incredibly space-efficient.

4. The Directed Acyclic Graph (DAG)

History in Git is not a linear timeline; it is a Directed Acyclic Graph (DAG).

  • Directed: Commits point to their parent(s).
  • Acyclic: You cannot have a commit that eventually points back to itself (you can't loop in time).

When you "branch" in Git, you aren't creating a copy of your files. You are simply creating a new path in the graph. When you "merge," you are creating a new commit that has two parents, effectively joining two paths of history.

5. References and HEAD

If hashes are 40-character strings like 4a8d1..., humans need a better way to navigate. References (or "refs") are human-readable pointers to hashes.

  • Branches: A branch (like main or feature-login) is simply a pointer to a specific commit hash. When you make a new commit, the branch pointer automatically moves forward to the new hash.
  • Tags: A tag is a pointer that does not move (e.g., v1.0).
  • HEAD: This is a special pointer that tells Git where you are currently working. Usually, HEAD points to a branch pointer, which in turn points to a commit.

6. The Three States of Git

To use Git effectively, you must understand the workflow between three distinct areas:

  1. Working Directory: The actual files you see and edit on your disk.
  2. Staging Area (Index): A "preview" of what will go into your next commit. This allows you to select specific changes rather than committing everything at once.
  3. Git Directory (Repository): The permanent database of snapshots (the .git folder).

The Workflow:

  1. Modify files in your Working Directory.
  2. git add <file>: Move changes to the Staging Area.
  3. git commit: Take the snapshot in the Staging Area and store it permanently in the Repository.

7. Essential Command Patterns

Mapping the commands to the model:

Command Action on the Model
git init Creates the .git directory and the initial data structures.
git add Creates blobs for files and updates the Staging Area (Index).
git commit Creates a new commit object and moves the current branch pointer to it.
git checkout <hash/branch> Moves HEAD to a different point in the graph and updates the Working Directory.
git merge <branch> Creates a new commit with two parents: the current HEAD and the target branch.

8. Advanced Intuition: Detached HEAD and Rebasing

  • Detached HEAD: This occurs when you checkout a specific commit hash rather than a branch. You are no longer "on" a branch. If you make commits here, they won't belong to any branch and may be lost unless you create a new branch pointer to save them.
  • Rebasing: Instead of merging two branches with a merge commit, rebasing "replays" your changes on top of another branch. In the DAG, this looks like picking up a segment of the graph and moving its base to a new starting point. This results in a cleaner, linear history but should be used with caution on shared branches.

9. Collaboration and Remotes

Git is a Distributed VCS. Every collaborator has a full copy of the repository's history.

  • Remote: A version of the project hosted on the internet (e.g., GitHub, GitLab).
  • Fetch: Download the latest objects and refs from the remote without changing your local work.
  • Pull: A combination of fetch and merge.
  • Push: Upload your local commits to the remote repository to share them with others.

By mastering the data model—blobs, trees, commits, and the DAG—you move from memorizing magic incantations to performing precise operations on a robust versioning engine.

Flashcards

  • What are the three primary objects in the Git data model?: Blobs (file content), Trees (directories), and Commits (snapshots with metadata).
  • What does it mean that Git is 'content-addressable'?: Every object is identified by a SHA-1 hash of its contents; if the content changes, the ID changes, ensuring data integrity.
  • What is the Directed Acyclic Graph (DAG) in Git?: A mathematical structure where commits point back to their parents, forming a history that never loops back on itself.
  • Explain the difference between a Branch and a Tag.: A branch is a movable pointer to a commit that advances automatically; a tag is a fixed pointer used for milestones (like v1.0).
  • What are the 'Three States' of Git?: 1. Working Directory (editing), 2. Staging Area/Index (preparing), 3. Git Directory/Repository (stored snapshots).

Study Guide

Git: The Data Model Approach

Git is more than a set of commands; it is a content-addressable key-value store that manages a Directed Acyclic Graph (DAG) of snapshots. By focusing on how Git stores data rather than memorizing flags, you can predict how commands like merge or rebase will behave.

Core Concepts

  • Blobs & Trees: A Blob stores file data (not the name). A Tree maps names to Blobs or other Trees, representing a directory structure.
  • Commits: These are snapshots of the top-level Tree. They include metadata like author, message, and pointers to parent commits, creating the history chain.
  • The DAG: Because commits point to parents, history is a graph. Branching is just creating a new path; merging is creating a commit with multiple parents.
  • References: Humans use names like 'main' or 'HEAD'. These are just text files containing a 40-character SHA-1 hash that point into the graph.
  • The Workflow: You modify files in the Working Directory, 'add' them to the Staging Area to curate the next snapshot, and 'commit' to move that snapshot into the permanent Repository.

Infographic

Infographic: Version Control and Git

Source Materials

Study The Missing Semester of Your CS Education on Lykke

This course page is free to open: signed out, you can read its first two sections, and signing in, on any plan, opens the rest. The free Lykke Chrome extension syncs your own Canvas courses, so ChatGPT, Claude and Muse can answer from your classes and deadlines. It only reads Canvas.

Add the free extensionSign in to open every section

All sections of The Missing Semester of Your CS Education · Browse more course pages