Working Tree, Staging Area and Repository

Updated · 11 min read
easyGit 2.53gitstaging-areaindexworking-treeinterview

Srihitha is sending a parcel to her cousin. Her desk is covered with things: books, a sweater, a half-written letter, old receipts. She doesn't tip the whole desk into a box. She picks the items that belong in this parcel and puts them in an open box. She checks the box, seals it, writes a label, and hands it over. The sealed parcel can't be changed anymore.

Git works in the same three steps. Your files are the desk. The open box is where you collect what goes into the next commit. The sealed parcel is the commit.

Git calls these the working tree, the staging area and the repository. Almost every Git command moves something between them, so this picture is the most useful thing you can learn about Git.

The Three Areas

A change travels from the working tree to the staging area to the repository
AreaWhat it isWhere it lives
Working treeThe project files you see and editYour project folder
Staging areaThe content that will go into your next commitOne file inside .git, named index
RepositoryEvery commit ever made in this projectThe .git folder

A change takes two steps to become part of the history:

  1. git add copies a change from the working tree into the staging area. This is called staging the change.
  2. git commit saves everything in the staging area as a new commit in the repository.

Watch a Change Move Through the Areas

Navika creates the Fee Tracker repository and two files. git status reports what is in each area:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo "class FeeCalculator {}" > FeeCalculator.java
git status
Output
On branch main
 
No commits yet
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	FeeCalculator.java
	README.md
 
nothing added to commit but untracked files present (use "git add" to track)

Both files are in the working tree only. Git sees them, calls them untracked, and does nothing with them until told.

The Java file is not ready, so Navika stages only the README:

Terminal
git add README.md
git status
Output
On branch main
 
No commits yet
 
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   README.md
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	FeeCalculator.java

README.md is now listed under "Changes to be committed". That heading is the staging area. The Java file stays where it was.

Now she commits:

Terminal
git commit -m "Add project README"
Output
[main (root-commit) 34d1952] Add project README
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
Terminal
git status
Output
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	FeeCalculator.java
 
nothing added to commit but untracked files present (use "git add" to track)

How it works

  1. The commit reports 1 file changed. Only the staged file went in, although two files were in the folder.
  2. README.md is now in the repository, saved in commit 34d1952.
  3. The staging area has nothing new in it, so git status no longer shows "Changes to be committed".
  4. FeeCalculator.java is still waiting in the working tree, untouched.

Why Git Has a Staging Area

Many tools save everything you changed in one step. Git puts a step in between, and it is one of the reasons developers like it.

  • You choose what belongs together. You fixed a bug and also tidied an unrelated file. With staging, these become two commits, each with a clear message, and either one can be undone alone.
  • Unfinished work stays out. A half-written file or a private experiment can sit in your folder for days without ending up in a commit.
  • You get a moment to check. Between git add and git commit, you can look at exactly what is about to be saved.

Think of the parcel again. Without the open box, every parcel would contain your whole desk.

The Staging Area Holds a Copy

Here is the detail that surprises almost everyone. git add copies the file as it is at that moment. If you change the file again afterward, the staging area still holds the earlier version.

Navika adds a line to the README, stages it, and then adds one more line:

Terminal
echo "Tracks school fee payments." >> README.md
git add README.md
echo "Built by Navika, Srihitha and Advitha." >> README.md
git status
Output
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   README.md
 
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:   README.md
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	FeeCalculator.java

README.md appears twice, and that is correct. There are now three versions of this one file:

AreaVersion of README.md
Repository (last commit)1 line
Staging area2 lines: the version at the moment of git add
Working tree3 lines: what is in the folder now

If she commits now, which version is saved?

Terminal
git commit -m "Describe what the app does"
git show HEAD:README.md
Output
[main 60dd037] Describe what the app does
 1 file changed, 1 insertion(+)
Fee Tracker
Tracks school fee payments.

git show HEAD:README.md prints the file as it is in the newest commit. It has two lines: the staged version. The third line is still only in the working tree, and git status would list the file as modified. To include it, she would run git add README.md again and make another commit.

Other Names You Will See

Git and its documentation use several names for the same areas. They all mean the same thing:

In this courseAlso called
Working treeWorking directory, working copy
Staging areaIndex, cache
RepositoryLocal repository, "the .git folder", history

This explains some option names. --staged and --cached both mean "the staging area", and the file that stores it is .git/index.

Common Mistakes

Expecting git commit to save everything in the folder. It saves only what is staged. If git commit answers "nothing added to commit", you forgot git add.

Editing after staging and not staging again. This is the mistake from the section above, and experienced developers make it too. Run git status before every commit.

Staging everything out of habit. git add . stages every change in the folder, wanted or not. It is fine when you have checked git status first, and it is how passwords and junk files get committed when you have not.

Thinking a staged file is safe. The staging area is a waiting room, not the history. Only a commit records the change with a message, an author and a date.

Thinking the staging area is on a server. All three areas are on your own computer. Nothing in this lesson needed a network.

Under the Hood

You can look inside the staging area with git ls-files --stage:

Terminal
git ls-files --stage
Output
100644 223bab126d5f9484c2d82349beacc6fe37c16951 0	README.md

The staging area is a list: one line for each tracked file, with the file's mode, the ID of its content, a stage number (0 in normal use) and its name.

Notice the ID. When Navika ran git add, Git stored the file's content inside .git right away, as a blob, and wrote the blob's ID in this list. git commit then has very little to do: it turns the list into a snapshot and writes a commit that points to it. The staging area is, quite literally, the next snapshot being prepared.

The list holds one entry and not two, because FeeCalculator.java was never added. A file that is not in this list is untracked.

Interview Corner

What are the three areas (or three trees) of Git?

The working tree holds the files you edit. The staging area (also called the index) holds the content prepared for the next commit. The repository (the .git folder) holds the committed history. git add moves changes from the working tree to the staging area, and git commit saves the staging area as a new commit.

What is the staging area, and why does Git have one?

It is an intermediate area that holds exactly what the next commit will contain. It lets you build a commit from selected changes, so that one commit holds one logical change, even if your working tree contains several unrelated edits. It also lets you review a commit before making it.

You run git add file.txt, edit the file again, and commit. What is in the commit?

The version from the moment of git add. Staging copies the content at that time. The later edit stays in the working tree as an unstaged change, and git status shows the file under both "Changes to be committed" and "Changes not staged for commit" until you add it again.

What is the difference between the index and the staging area?

There is none. "Index" is the technical name, and it is stored in the file .git/index. "Staging area" describes what it is used for. Some options still say "cached", which means the same.

Practice

Challenge: stage, edit, commit. In the same repository, create fees.txt with the line Term 1 fee: $12,000 and stage it. Then add a second line, Term 2 fee: $12,500, and commit without staging again. Print the committed file with git show HEAD:fees.txt.

What does Git print? Before you open the solution, decide how many lines the committed fees.txt has.

Show solution
Terminal
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
echo 'Term 2 fee: $12,500' >> fees.txt
git commit -m "Add fee list"
git show HEAD:fees.txt
Output
[main 3122adb] Add fee list
 1 file changed, 1 insertion(+)
 create mode 100644 fees.txt
Term 1 fee: $12,000

One line. The commit took the staged copy, and the second line exists only in the working tree. Run git status to see fees.txt listed as modified, then stage and commit it to finish the job.

Quick check: The three areas

4 questions
1What does git add do?
2What does a commit contain?
3git status lists README.md under both "Changes to be committed" and "Changes not staged for commit". Why?
4Which of these is another name for the staging area?

Key Takeaways

Quick reference

CommandIt is used to
git statusShow what is in the working tree and the staging area
git add <file>Copy a file's current content into the staging area
git commit -m "message"Save the staging area as a new commit
git show HEAD:<file>Print a file as it is in the newest commit
git ls-files --stageList the content of the staging area

Was this tutorial helpful?

Lesson 9 of 106