Working Tree, Staging Area and Repository
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
| Area | What it is | Where it lives |
|---|---|---|
| Working tree | The project files you see and edit | Your project folder |
| Staging area | The content that will go into your next commit | One file inside .git, named index |
| Repository | Every commit ever made in this project | The .git folder |
A change takes two steps to become part of the history:
git addcopies a change from the working tree into the staging area. This is called staging the change.git commitsaves 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:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo "class FeeCalculator {}" > FeeCalculator.java
git statusOn 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:
git add README.md
git statusOn 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.javaREADME.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:
git commit -m "Add project README"[main (root-commit) 34d1952] Add project README
1 file changed, 1 insertion(+)
create mode 100644 README.mdgit statusOn 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
- The commit reports
1 file changed. Only the staged file went in, although two files were in the folder. README.mdis now in the repository, saved in commit34d1952.- The staging area has nothing new in it, so
git statusno longer shows "Changes to be committed". FeeCalculator.javais 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 addandgit 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:
echo "Tracks school fee payments." >> README.md
git add README.md
echo "Built by Navika, Srihitha and Advitha." >> README.md
git statusOn 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.javaREADME.md appears twice, and that is correct. There are now three versions of this one file:
| Area | Version of README.md |
|---|---|
| Repository (last commit) | 1 line |
| Staging area | 2 lines: the version at the moment of git add |
| Working tree | 3 lines: what is in the folder now |
If she commits now, which version is saved?
git commit -m "Describe what the app does"
git show HEAD:README.md[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 course | Also called |
|---|---|
| Working tree | Working directory, working copy |
| Staging area | Index, cache |
| Repository | Local 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:
git ls-files --stage100644 223bab126d5f9484c2d82349beacc6fe37c16951 0 README.mdThe 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
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[main 3122adb] Add fee list
1 file changed, 1 insertion(+)
create mode 100644 fees.txt
Term 1 fee: $12,000One 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.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git status | Show 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 --stage | List the content of the staging area |
Was this tutorial helpful?