Repositories and the Working Tree
Introduction
Git organizes your project into three main areas: the working tree (files you edit), the staging area (changes selected for the next commit), and the repository (saved history in .git). Understanding this flow makes git status, git add, and git commit predictable instead of mysterious.
Prerequisites
What Is a Repository?
A repository (repo) is a project folder plus Git history. The hidden .git directory stores all commits, branches, and tags:
my-app/
├── .git/ ← Git database (do not edit by hand)
├── src/
│ └── app.js
└── README.mdCode explanation:
- Delete
.gitand you lose history—you keep only current files - Clone or init creates
.git
Three Trees (Conceptual)
Working tree Staging area Repository
(your files) (index) (commits)
│ │ │
│ git add │ │
├────────────────────►│ │
│ │ git commit │
│ ├─────────────────────►│
│ │ │
│ git restore │ │
│◄────────────────────┤ │| Area | Also called | What it holds |
|---|---|---|
| Working tree | Working directory | Files on disk as you edit them |
| Staging area | Index, cache | Snapshot of changes for next commit |
| Repository | HEAD history | Committed snapshots |
File States
| State | Meaning | git status label |
|---|---|---|
| Untracked | New file Git never saw | Untracked files |
| Modified | Changed since last commit, not staged | Changes not staged |
| Staged | Marked for next commit | Changes to be committed |
| Committed | Saved in history | Clean until you edit again |
Typical flow:
untracked → (add) → staged → (commit) → committed
↑
modified (edit again)Create a New Repository
mkdir hello-git
cd hello-git
git initCode explanation:
git initcreates.gitin the current folder- You now have an empty repo on the default branch (
main)
Check:
git statusExpected: On branch main, No commits yet, nothing to commit.
Clone an Existing Repository
Download a full copy including history:
git clone https://github.com/example/hello.git
cd helloCode explanation:
clone= init + remote + fetch + checkout in one step- Creates
hello/with.gitand working files
SSH form (after keys configured):
git clone git@github.com:example/hello.gitBare vs Non-Bare (Awareness)
| Type | Working files? | Use |
|---|---|---|
| Non-bare (normal) | Yes | Your laptop, daily dev |
| Bare | No—only .git data | Central server / git push target |
You work in non-bare repos. Hosting servers store bare repos internally.
Read git status
After creating a file:
echo "# Hello Git" > README.md
git statusSample output:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files presentCode explanation:
- Untracked — Git sees
README.mdbut does not version it yet - Next step:
git add(next chapter)
Short status:
git status -sHEAD and Commits (Preview)
HEAD points to your current branch tip—usually the latest commit on main. Each commit is a snapshot identified by a hash:
a1b2c3d4... (short: a1b2c3d)After your first commit, HEAD moves forward with each new commit. Branches are movable pointers to commits—chapter 8 expands this.
Objects Under the Hood (Awareness)
Git stores blob (file content), tree (directory listing), commit (metadata + tree), tag (named commit). You rarely touch these directly—commands like add and commit build them automatically.
Why branches are cheap: a branch is a small pointer, not a folder copy.
FAQ
Is .git backed up?
Back up your repo folder or push to remote—.git is the history.
Can I rename the project folder?
Yes—paths outside .git do not affect Git metadata.
git init in wrong folder?
Delete .git with rm -rf .git only if you have no commits you need—or start fresh folder.
Difference between clone and fork?
Fork is a platform action (GitHub copy); clone is Git download to your machine.
Why stage before commit?
Lets you commit part of your changes and review what goes in one snapshot.
Repository vs working tree size?
History in .git grows with commits; large binaries need LFS or they bloat the repo.