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.
- 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
2.2 Initializing a Project (`git init`) & Inspecting State (`git 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- 1.mkdir my-ai-project creates a fresh directory.
- 2.cd my-ai-project changes your terminal's current working directory.
- 3.git init scaffolds the .git subfolder.
- 4.git status prints current branch state and shows 'No commits yet'.
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)
• 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`)
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"- 1.echo writes '# My AI Project' into README.md.
- 2.git status -s uses short format: '?? README.md' indicates an untracked file.
- 3.git add stages the file: status changes to 'A README.md' (Added).
- 4.git commit packages the staged index into commit snapshot [main (root-commit)].
[main (root-commit) a1b2c3d] docs: add initial project README 1 file changed, 1 insertion(+) create mode 100644 README.md
• 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`)
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- 1.The cat command writes standard ignore rules into .gitignore.
- 2.git add .gitignore stages the rule file.
- 3.git commit commits the configuration.
- 4.git log --oneline displays the chronological history of commits from newest to oldest.
* 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
basicIn 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
- 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
git initInitializes a new Git repository in the current directorygit statusDisplays the state of the working directory and the staging areagit 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 messagegit log --onelineDisplays commit history with one commit per line including short SHA-1 hashgit diffShows unstaged differences between your working directory and the staging areaKnowledge 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?