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.
- 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:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch mainThat 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:
1. Working Folder
Edit files
2. Staging Area
Ready box
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:
- Working Directory: Your normal project folder on disk where you create, edit, and delete files in your editor.
- 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. - Repository History (
git commit): The sealed archive hidden inside.git/. Once you rungit 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:
# 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 statusRight 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:
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:
node_modules/
.next/
__pycache__/
.env
*.log
.DS_StoreAdd 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:
git diff
git log --onelineDescribe 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
What is the purpose of git add?