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:

text
my-app/
├── .git/          ← Git database (do not edit by hand)
├── src/
│   └── app.js
└── README.md

Code explanation:

  • Delete .git and you lose history—you keep only current files
  • Clone or init creates .git

Three Trees (Conceptual)

text
  Working tree          Staging area           Repository
  (your files)          (index)                (commits)
       │                     │                      │
       │  git add            │                      │
       ├────────────────────►│                      │
       │                     │  git commit          │
       │                     ├─────────────────────►│
       │                     │                      │
       │  git restore        │                      │
       │◄────────────────────┤                      │
AreaAlso calledWhat it holds
Working treeWorking directoryFiles on disk as you edit them
Staging areaIndex, cacheSnapshot of changes for next commit
RepositoryHEAD historyCommitted snapshots

File States

StateMeaninggit status label
UntrackedNew file Git never sawUntracked files
ModifiedChanged since last commit, not stagedChanges not staged
StagedMarked for next commitChanges to be committed
CommittedSaved in historyClean until you edit again

Typical flow:

text
untracked → (add) → staged → (commit) → committed

         modified (edit again)

Create a New Repository

bash
mkdir hello-git
cd hello-git
git init

Code explanation:

  • git init creates .git in the current folder
  • You now have an empty repo on the default branch (main)

Check:

bash
git status

Expected: On branch main, No commits yet, nothing to commit.

Clone an Existing Repository

Download a full copy including history:

bash
git clone https://github.com/example/hello.git
cd hello

Code explanation:

  • clone = init + remote + fetch + checkout in one step
  • Creates hello/ with .git and working files

SSH form (after keys configured):

bash
git clone git@github.com:example/hello.git

Bare vs Non-Bare (Awareness)

TypeWorking files?Use
Non-bare (normal)YesYour laptop, daily dev
BareNo—only .git dataCentral server / git push target

You work in non-bare repos. Hosting servers store bare repos internally.

Read git status

After creating a file:

bash
echo "# Hello Git" > README.md
git status

Sample output:

text
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 present

Code explanation:

  • Untracked — Git sees README.md but does not version it yet
  • Next step: git add (next chapter)

Short status:

bash
git status -s

HEAD 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:

text
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.