2. Git Core Concepts

Git Core Model: Working Tree, Staging & Commits

Understand Git's 3-stage mental model, initialize a repository, track changes with git status/add/commit, ignore junk files with .gitignore, and read git log.

3 min readUpdated 2026-11-29
In This Lesson
  • Understand Git's Working Folder, Staging Area, and Repository zones
  • Initialize a repository and record clean commits with git add and git commit
  • Exclude build artifacts and secrets using .gitignore

Have you ever saved files like resume_final.docx, resume_final_v2.docx, and resume_ACTUAL_final.docx just because you were afraid of losing yesterday's working draft? Git eliminates that habit forever—it works like an intelligent time machine for your entire project folder, recording clean snapshots you can inspect or revisit at any time.

Setting Your Identity Once Per Machine

Every snapshot you record in Git gets stamped with an author name and email. Before creating your first repository, introduce yourself to Git once globally so every project on your laptop inherits it:

bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

That third line ensures every new repository starts with main as its default branch name instead of older legacy names.

Git's 3-Zone Mental Model

Most beginners get tripped up in Git because they expect a single "Save" button, whereas Git uses two deliberate steps (git add, then git commit) across three zones:

Git's 3-Zone Workflow

1. Working Folder

Edit files

git add →

2. Staging Area

Ready box

git commit →

3. Repository (.git)

Saved history

Edit files in Working Directory, stage chosen changes with `git add`, and save snapshots with `git commit`.

Think of packing items to ship home:

  1. Working Directory: Your normal project folder on disk where you create, edit, and delete files in your editor.
  2. Staging Area (git add): Your open shipping box. You place only the files that belong together in this snapshot into the box, leaving unfinished work out for later.
  3. Repository History (git commit): The sealed archive hidden inside .git/. Once you run git commit, that exact state of the box is permanently saved with a message and a timestamp.

Let's walk through creating a repository and sealing your first commit:

bash
# Turn the current folder into a Git repository starting on branch 'main'
git init -b main

# Check what Git sees: untracked files, edited files, and what is staged
git status

Right after git init, git status shows your files in red under Untracked files. Move app.py into the staging box, check git status again (now green under Changes to be committed), and seal the snapshot:

bash
git add app.py
git commit -m "Add initial app script"

Keeping Junk Out with .gitignore

Modern projects generate thousands of temporary or secret files—such as downloaded packages (node_modules/), build output (.next/), Python caches (__pycache__/), OS thumbnails (.DS_Store), and local passwords (.env). You never want those cluttering your Git history.

Create a plain text file named .gitignore in the root folder of your repository and list patterns Git should quietly skip:

gitignore
node_modules/
.next/
__pycache__/
.env
*.log
.DS_Store

Add and commit .gitignore early so your team stays protected automatically.

Reading Diffs and History Before You Commit

Two lightweight commands keep you confident before every commit. git diff prints the exact green + lines added and red - lines removed since your last commit, letting you catch stray print() statements or typos before staging. Afterward, git log --oneline displays a tidy timeline of every snapshot in your project:

bash
git diff
git log --oneline
Write Clear Commit Messages

Describe what applying this commit does in short present tense—future you (and your teammates) will thank you when scanning git log --oneline:

  • Clear: "Add search modal keyboard shortcut"
  • Unhelpful: "stuff", "wip", or "fix"

Quick Checkpoints

Check Your Understanding

What is the purpose of git add?