ProgramGuru

Understand the working tree, staging area and repository

Understand the working tree, staging area and repository

Most beginner Git confusion comes from not knowing which version of a file a command is talking about. Git lets the same path exist in three useful states: your current file, the staged version and the committed version.

What you’ll learn

  • Distinguish working, staged and committed content.
  • Explain why a file can have staged and unstaged changes.
  • Use the staging area intentionally.

The three everyday states

  1. Working tree

    Edit files.

    current edits
  2. Staging area

    Select exact content for the next commit.

    git add
  3. Repository

    Record the staged snapshot.

    git commit
git commit

The staging area lets you choose the next commit

The working tree is what you edit. The staging area, also called the index, is the proposed content for the next commit. HEAD usually refers to the current committed snapshot.

Because staging is separate, you can edit several files but commit only the subset that belongs together. This supports small, understandable commits.

Create different working and staged versions

Run this in a disposable practice repository unless the lesson explicitly says otherwise.

printf "Buy milk\n" > notes.txt
git add notes.txt
printf "Call supplier\n" >> notes.txt
git status
git diff
git diff --staged

After git add, the first line is staged. Appending another line changes the working tree again. git diff shows the unstaged difference, while git diff --staged shows what the next commit would contain compared with HEAD.

Practice: Working tree and staging

Your task: Stage a file, edit it again, and explain why git status can show the same path under both 'Changes to be committed' and 'Changes not staged for commit.'

Common pitfall to avoid

Running git add is not 'saving forever.' It updates the proposed next snapshot. A commit is the durable history checkpoint.

Check your understanding

What does the staging area represent?
Choose one answer. You can try again.

Answer and explanation

The exact content proposed for the next commit.

Correct.

Working tree and staging: what to remember

Understand the working tree, staging area and repository is complete when you can explain the state being changed before you run the command, not merely repeat the syntax.

Read further: official supporting documentation.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Stage changes and create commits →