The three areas

Topic 2 of 5 in Your first repository8 min readBeginnerFree course

Overview

Most version control systems have two places: your files and the history. Git has three, and understanding them is the single most important step in learning Git. Once you can picture these three areas, almost every command makes sense.

The three areas of a Git project. The working directory holds the files you edit, such as an edited index.html and style.css. The staging area holds the next commit being built, here index.html. The repository holds commits — saved snapshots. git add moves changes from the working directory to the staging area; git commit saves the staging area as a commit in the repository; git restore and git switch bring files back out into the working directory.

1. The working directory

The files you see and edit in your folder. You change them with your editor; Git just watches. Changes here are not saved in Git — if you delete a file by mistake before committing, Git can only give you back the last committed version.

2. The staging area

Also called the index. It’s a draft of your next commit. You decide what goes into it with git add:

Terminal
git add index.html          # stage one file
git add css/                # stage everything in a folder
git add .                   # stage everything in the current folder and below

git add takes a copy of the file as it is right now. If you edit the file again afterwards, the new edit is not staged until you git add again — the staged copy and the working copy can differ.

3. The repository

The history: every commit you’ve made, stored in .git. git commit takes everything in the staging area and saves it as a new commit:

Terminal
git commit -m "Add welcome message"

Once committed, a snapshot is safe. You can always get back to it.

Why have a staging area?

It lets you choose what goes into each commit. Say you’ve spent an hour on a feature and also fixed an unrelated typo along the way. Rather than one commit labelled “stuff”, you can make two clean ones:

Terminal
git add README.md
git commit -m "Fix typo in installation steps"

git add src/search.js src/search.test.js
git commit -m "Add search by title"

Small, focused commits are easier to review, easier to understand later, and easier to undo on their own.

You can even stage part of a file with git add -p (patch mode): Git shows each changed chunk (“hunk”) and asks whether to stage it. It’s a favourite of experienced developers for splitting mixed changes.

Reading git status

git status shows all three areas at once:

Text
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   index.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   style.css

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	notes.txt
  • Changes to be committed — staged; they will go into the next commit.
  • Changes not staged for commit — modified in the working directory, not staged.
  • Untracked files — new files Git isn’t tracking.

The short format packs this into two columns:

Terminal
git status --short
M  index.html
 M style.css
?? notes.txt

The life of a file in Git. An untracked file — new, and ignored by Git — becomes staged with git add, meaning it is in the next commit. git commit makes it unmodified, the same as the last commit. Editing it makes it modified; git add stages it again, and git restore takes it back to unmodified. git status –short shows ?? new.txt for untracked, A staged.txt for a newly staged file and M modified.txt for a modified one.

The left column is the staging area, the right column is the working directory. M = modified, A = added (new file, staged), D = deleted, R = renamed, ?? = untracked. A file can appear in both columns: MM means “staged, then edited again”.

Moving changes back out: git restore

You’ll often want to undo something in one of the areas. git restore does it:

You want to… Command
discard your edits to a file (back to the staged or committed version) git restore style.css
unstage a file but keep your edits git restore --staged index.html
both — throw away staged and unstaged edits git restore --staged --worktree index.html

git restore <file> permanently discards your uncommitted edits to that file. Git never had a copy of them, so there’s no undo. Check with git diff <file> first if you’re not sure.

git restore arrived in Git 2.23 (2019). In older tutorials you’ll see git checkout -- <file> and git reset HEAD <file> for the same jobs; they still work, but restore is clearer.

A mental model

Think of a photographer:

  • the working directory is the scene, which people keep moving around;
  • the staging area is the frame you’ve lined up in the viewfinder;
  • committing is pressing the shutter — the photo goes into the album (the repository) and can’t be changed.

You can rearrange the frame as much as you like before pressing the shutter. After that, you’d take a new photo.

Habit to build: git status → git add specific files → git status again → git commit. Checking status before committing catches most “oops, I didn’t mean to commit that” moments.

Exercise

Stage exactly what you mean

The script sets up a repository with one commit, then makes three changes: edits index.html, edits style.css and creates notes.txt. Stage only the index.html change and check with git status --short that the codes are M for index.html, M for style.css and ?? for notes.txt. Then you change your mind about style.css: discard that edit so the file is back to its committed version. Finally, unstage index.html again without losing the edit.

Terminal
mkdir areas && cd areas && git init -b main
echo "<h1>Home</h1>" > index.html
echo "h1 { color: navy; }" > style.css
git add . && git commit -m "Initial site"

echo "<p>Welcome!</p>" >> index.html
echo "h1 { color: red; }" > style.css
echo "remember to add a contact page" > notes.txt

# 1. Stage only index.html and check the short status

# 2. Throw away the style.css edit

# 3. Unstage index.html but keep the edit in the file

Summary

  • The working directory is your files, the staging area is the next commit being built, and the repository is the saved history.
  • git add copies changes into the staging area; git commit saves the staged snapshot as a commit.
  • Staging lets you commit some changes and not others, so each commit is one focused change.
  • git restore <file> discards edits in the working directory; git restore --staged <file> unstages without losing edits.
  • In git status --short, the left column is the staging area and the right column is the working directory.