Bird
Raised Fist0
Gitdevops~5 mins

Why three areas matter (working directory, staging, repository) in Git - Why It Works

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
When you work with files in a project, you need a way to track changes safely. Git uses three areas to help you manage your work: the working directory, the staging area, and the repository. This setup helps you control what changes are saved and shared.
When you want to edit files but only save some changes permanently.
When you want to review your changes before saving them.
When you want to keep a history of your project versions.
When you want to undo changes before saving them.
When you want to share your saved changes with others.
Commands
This command shows the current state of your working directory and staging area. It tells you which files are changed, staged, or untracked.
Terminal
git status
Expected OutputExpected
On branch main Your branch is up to date with 'origin/main'. 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: example.txt no changes added to commit (use "git add" and/or "git commit -a")
This command moves the changes in example.txt from the working directory to the staging area. It means you are ready to save these changes.
Terminal
git add example.txt
Expected OutputExpected
No output (command runs silently)
Run git status again to see that example.txt is now staged and ready to be committed.
Terminal
git status
Expected OutputExpected
On branch main Your branch is up to date with 'origin/main'. Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: example.txt
This command saves the staged changes into the repository with a message describing the update.
Terminal
git commit -m "Update example.txt with new info"
Expected OutputExpected
[main abc1234] Update example.txt with new info 1 file changed, 3 insertions(+), 1 deletion(-)
-m - Adds a commit message inline without opening an editor
Check the status again to confirm there are no changes left in the working directory or staging area after the commit.
Terminal
git status
Expected OutputExpected
On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
Key Concept

If you remember nothing else from this pattern, remember: the working directory is where you edit files, the staging area is where you prepare changes, and the repository is where changes are saved permanently.

Common Mistakes
Trying to commit changes without adding them to the staging area first.
Git will not include those changes in the commit because they are not staged.
Use 'git add <file>' to stage changes before committing.
Adding all files without checking what changes are staged.
You might commit unwanted changes or mistakes.
Use 'git status' to review changes before staging and committing.
Editing files and expecting them to be saved in the repository immediately.
Changes stay only in the working directory until staged and committed.
Stage changes with 'git add' and then commit with 'git commit'.
Summary
Use 'git status' to see changes in the working directory and staging area.
Use 'git add' to move changes from working directory to staging area.
Use 'git commit' to save staged changes into the repository.

Practice

(1/5)
1. Which area in Git is where you make changes to your files before saving them?
easy
A. Repository
B. Staging area
C. Working directory
D. Remote server

Solution

  1. Step 1: Understand the role of the working directory

    The working directory is the place on your computer where you edit and change files.
  2. Step 2: Differentiate from other areas

    The staging area is for selecting changes to save, and the repository stores saved snapshots. Changes happen first in the working directory.
  3. Final Answer:

    Working directory -> Option C
  4. Quick Check:

    Changes start in working directory = A [OK]
Hint: Changes happen first in working directory before staging [OK]
Common Mistakes:
  • Confusing staging area with where changes are made
  • Thinking repository is where changes are edited
  • Mixing remote server with local areas
2. Which Git command moves changes from the working directory to the staging area?
easy
A. git commit
B. git clone
C. git push
D. git add

Solution

  1. Step 1: Identify the command for staging changes

    The command git add is used to move changes from the working directory to the staging area.
  2. Step 2: Differentiate from other commands

    git commit saves staged changes to the repository, git push sends commits to remote, and git clone copies a repo.
  3. Final Answer:

    git add -> Option D
  4. Quick Check:

    Staging uses git add = B [OK]
Hint: Use git add to stage changes before committing [OK]
Common Mistakes:
  • Using git commit to stage changes
  • Confusing git push with staging
  • Thinking git clone stages files
3. What is the output of the following sequence of commands?
echo "Hello" > file.txt
git add file.txt
git commit -m "Add file"
git status
medium
A. On branch main nothing to commit, working tree clean
B. On branch main Changes to be committed: new file: file.txt
C. On branch main Untracked files: file.txt
D. fatal: not a git repository

Solution

  1. Step 1: Understand the command sequence

    First, a file is created with "Hello" content. Then git add stages it. git commit saves it to the repository.
  2. Step 2: Interpret git status output

    After commit, there are no changes left unstaged or uncommitted, so status shows "nothing to commit, working tree clean".
  3. Final Answer:

    On branch main nothing to commit, working tree clean -> Option A
  4. Quick Check:

    Committed changes clear status = A [OK]
Hint: After commit, git status shows clean working tree [OK]
Common Mistakes:
  • Expecting staged changes after commit
  • Confusing untracked with committed files
  • Ignoring that commit clears staging
4. You ran git commit but Git says "nothing to commit, working tree clean". What is the most likely reason?
medium
A. You are not inside a Git repository
B. You forgot to stage changes with git add
C. You have uncommitted changes in the repository
D. You have already pushed changes to remote

Solution

  1. Step 1: Analyze the message meaning

    "Nothing to commit, working tree clean" means no staged changes are ready to be saved.
  2. Step 2: Identify the cause

    If you made changes but did not stage them with git add, commit finds nothing to save.
  3. Final Answer:

    You forgot to stage changes with git add -> Option B
  4. Quick Check:

    Stage changes before commit = C [OK]
Hint: Always git add before git commit to save changes [OK]
Common Mistakes:
  • Assuming commit saves unstaged changes
  • Thinking remote push affects local commit
  • Confusing repository state with staging
5. You modified two files: index.html and style.css. You want to save only index.html changes in the next commit. Which sequence correctly does this?
hard
A. git add index.html git commit -m "Save index.html changes"
B. git commit -m "Save index.html changes"
C. git commit -a -m "Save index.html changes"
D. git add style.css git commit -m "Save index.html changes"

Solution

  1. Step 1: Understand staging selective changes

    To commit only index.html, you must stage it with git add index.html and not stage style.css.
  2. Step 2: Analyze options

    git add style.css git commit -m "Save index.html changes" stages the wrong file. git commit -a -m "Save index.html changes" commits all tracked changes (both files). git commit -m "Save index.html changes" commits nothing if no staged changes exist.
  3. Final Answer:

    git add index.html git commit -m "Save index.html changes" -> Option A
  4. Quick Check:

    Stage only desired file before commit = D [OK]
Hint: Stage only files you want to commit with git add [OK]
Common Mistakes:
  • Staging wrong file by mistake
  • Using git commit -a to commit all changes
  • Committing without staging any file