Ch 02The Three Trees: Initializing, Staging & Commits
Chapter 02 of 05·25 min

The Three Trees: Initializing, Staging & Commits

To master Git, you must understand its internal mental model: **The Three Trees**. Unlike other version control systems that track file deltas, Git manages three distinct areas: your Working Directory, the Staging Area (Index), and the Git Repository history. In this chapter, you will initialize your first repository from scratch, inspect repository status, stage files with surgical precision, write atomic commit messages adhering to industry standards, and configure a production-grade .gitignore file.

Learning objectives
  • 01Visualize and navigate Git's Three Trees: Working Directory, Staging Index, and Git Repository Object Store (.git)
  • 02Initialize local repositories using git init and inspect the hidden .git metadata structure
  • 03Interpret git status outputs across untracked, modified, staged, and clean working tree states
  • 04Stage specific files using git add <file> and bulk stage projects using git add .
  • 05Author meaningful, atomic commit snapshots with git commit -m using the Conventional Commits specification
  • 06Protect repositories from committing sensitive secrets, binary artifacts, and cache files using .gitignore
  • 07Examine commit log history with git log --oneline --graph --decorate

2.1 The Three Trees Architecture: Working Directory, Staging Area & Repository

Every file in a Git project lives in one of three states: 1. **The Working Directory**: The actual files on your filesystem that you open in VS Code, edit, and run. These files are untracked or modified until explicitly staged. 2. **The Staging Area (The Index)**: A staging buffer where you prepare the exact snapshot of changes that will go into your next commit. This unique middle-layer lets you selectively group related changes instead of committing half-baked code. 3. **The Git Repository (.git)**: The permanent database of immutable commit objects, trees, and blobs stored inside the hidden `.git` folder in your project root.
Why Staging Matters
The staging area gives you architectural control. If you edited 5 files but only 2 of them belong to a bug fix, you can stage only those 2 (`git add file1.py file2.py`) and commit them as a clean, single-purpose unit.

2.2 Initializing a Project (`git init`) & Inspecting State (`git status`)

To place an existing folder under Git version control, navigate into that folder in your terminal and execute `git init`. This creates a hidden `.git` directory containing Git's internal object database, branch references (HEAD), and config files. The most important command you will run in your day-to-day work is `git status`. It answers three fundamental questions: • Which branch are you currently on? • What changes are staged for commit? • What files are modified or untracked?
Terminal Command 1: Initialize a New Repository and Inspect Status

Create a project directory, initialize Git tracking, and verify initial repository state.

# Create and enter a new project directory
mkdir my-ai-project
cd my-ai-project

# Initialize a new Git repository
git init

# Check the initial repository status
git status
Execution walkthrough
  1. 1.mkdir my-ai-project creates a fresh directory.
  2. 2.cd my-ai-project changes your terminal's current working directory.
  3. 3.git init scaffolds the .git subfolder.
  4. 4.git status prints current branch state and shows 'No commits yet'.
Expected output
Initialized empty Git repository in /Users/you/my-ai-project/.git/
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)
Edge cases

• Running git init inside a folder that is ALREADY inside another Git repository creates an accidental nested repo. Always verify with git status before running git init.

Why it happens: A newly initialized repo has no commits and an empty staging index.

2.3 Staging Files (`git add`) and Creating Snapshots (`git commit`)

When you create a file (e.g., `app.py` or `README.md`), Git recognizes it as an **untracked file**. It will not be included in version history until you move it to the Staging Index using `git add`. Once files are staged, you take a snapshot of that exact moment using `git commit -m "<message>"`. Commits are permanently recorded with a unique SHA-1/SHA-256 hash, author timestamp, and commit message. ### The Conventional Commits Standard Industry engineering teams follow structured commit messages: • `feat: add email threat scoring function` • `fix: resolve zero division bug in metric calculation` • `docs: update setup instructions in README.md` • `refactor: clean up loop syntax in data preprocessor`
Terminal Command 2: Create, Stage, and Commit a File

Create a project README, stage it in the index, and commit with an atomic message.

# Create a README markdown file
echo "# My AI Project" > README.md

# Inspect status to see untracked file
git status -s

# Stage the file into the index
git add README.md

# Verify file is staged (green 'A' marker)
git status -s

# Commit the staged snapshot
git commit -m "docs: add initial project README"
Execution walkthrough
  1. 1.echo writes '# My AI Project' into README.md.
  2. 2.git status -s uses short format: '?? README.md' indicates an untracked file.
  3. 3.git add stages the file: status changes to 'A README.md' (Added).
  4. 4.git commit packages the staged index into commit snapshot [main (root-commit)].
Expected output
[main (root-commit) a1b2c3d] docs: add initial project README
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
Edge cases

• If you run git commit without -m, Git opens a terminal text editor (Vim or Nano) to prompt for a message. To exit Vim: type ':wq' and hit Enter. To exit Nano: press 'Ctrl + O' then Enter, then 'Ctrl + X'.

Why it happens: Git reports the short hash (a1b2c3d), branch name, commit title, and summary of insertions and files created.

2.4 Protecting Repositories with `.gitignore` & Inspecting History (`git log`)

Never commit secrets (API keys, passwords), dependencies (`node_modules/`, `venv/`), or OS cache files (`.DS_Store`, `Thumbs.db`). A `.gitignore` file contains wildcard patterns telling Git which files and folders to completely ignore. To view your project history, run `git log`. Adding `--oneline --graph --decorate` produces a clean, readable ASCII branch tree.
Terminal Command 3: Configure .gitignore and View Commit History

Create a standard .gitignore file, stage and commit it, and inspect the project log tree.

# Create a standard .gitignore for Python and environment files
cat << 'EOF' > .gitignore
# Python virtual environments & bytecode
__pycache__/
*.py[cod]
.venv/
env/

# Environment secrets & local config
.env
.env.local

# Operating system files
.DS_Store
Thumbs.db
EOF

# Stage and commit .gitignore
git add .gitignore
git commit -m "chore: add .gitignore rules for virtual environments and secrets"

# Display pretty one-line commit history
git log --oneline --graph --decorate
Execution walkthrough
  1. 1.The cat command writes standard ignore rules into .gitignore.
  2. 2.git add .gitignore stages the rule file.
  3. 3.git commit commits the configuration.
  4. 4.git log --oneline displays the chronological history of commits from newest to oldest.
Expected output
* b4c5d6e (HEAD -> main) chore: add .gitignore rules for virtual environments and secrets
* a1b2c3d docs: add initial project README

Why it happens: The asterisk (*) shows the graph line, HEAD -> main shows the active branch pointer, and the hexadecimal hashes uniquely identify each snapshot.

Practice exercise

Work through the exercise before starting the chapter assignment

Exercise 2: Practice the Full Git Lifecycle

basic

In your terminal inside your project folder, create a new file named 'main.py' containing print('Hello Git'), stage it, and commit it with the message 'feat: implement main entry point'. Then view your log.

Chapter summary

Key concepts and syntax cards for review

Key concepts
  • 01The Three Trees: Working Directory (untracked/modified), Staging Index (prepared snapshot), and Git Repository (.git database)
  • 02git init initializes a new repository by creating the hidden .git directory
  • 03git status is the central command to inspect the current state of files across the three trees
  • 04git add stages changes into the Index; git commit converts the staged Index into an immutable commit object
  • 05.gitignore prevents accidental commits of secrets, virtual environments, and system junk files
  • 06git log --oneline --graph visualizes the history of commit snapshots in reverse chronological order
Syntax reference
git initInitializes a new Git repository in the current directory
git statusDisplays the state of the working directory and the staging area
git add <path>Stages file changes at <path> into the staging area (use . for all changes in current directory)
git commit -m "<message>"Records staged changes as a new commit snapshot with an explanatory message
git log --onelineDisplays commit history with one commit per line including short SHA-1 hash
git diffShows unstaged differences between your working directory and the staging area

Knowledge check

Verify your grasp of the chapter concepts

What does the command 'git status' output when a newly created file has not yet been staged?

Which command shows the concise one-line commit log graph?