Skip to content

Git Working Directory, Staging Area, and Repository

This chapter dives deep into Git's three core concepts: Working Directory, Staging Area, and Repository. Understanding these three areas is key to mastering Git.

Overview of the Three Areas

┌─────────────────────────────────────────────────────────────────┐
│                        Git Project Structure                    │
├─────────────────────────────────────────────────────────────────┤
│  Working Directory                                              │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  • Actual project files                                 │    │
│  │  • Can edit, create, delete files                       │    │
│  │  • Git monitors but doesn't manage directly             │    │
│  └─────────────────────────────────────────────────────────┘    │
│                              │                                  │
│                         git add                                 │
│                              ↓                                  │
│  Staging Area (Index)                                           │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  • Temporary snapshot for next commit                   │    │
│  │  • Precise control over commit content                  │    │
│  │  • Located in .git/index file                           │    │
│  └─────────────────────────────────────────────────────────┘    │
│                              │                                  │
│                        git commit                               │
│                              ↓                                  │
│  Repository                                                     │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  • Stores complete project history                      │    │
│  │  • Contains all commits, branches, tags                 │    │
│  │  • Located in .git directory                            │    │
│  └─────────────────────────────────────────────────────────┘    │
└─────────────────────────────────────────────────────────────────┘

Working Directory

Definition and Characteristics

The Working Directory is where you actually do your work. It contains all project files and folders (except the .git directory).

Characteristics:

  • 📁 Contains actual project files
  • ✏️ Freely edit, create, delete files
  • 👀 Git monitors changes but doesn't auto-save
  • 🔄 Can restore to any historical version at any time

Working Directory Example

bash
# Create demo project
mkdir git-areas-demo
cd git-areas-demo
git init

# Create files in working directory
echo "# Project Description" > README.md
echo "print('Hello, World!')" > main.py
mkdir src
echo "# Helper functions" > src/utils.py

# Check status
git status

Output:

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.md
        main.py
        src/

nothing added to commit but untracked files present (use "git add" to track)

Working Directory File States

Files in the working directory can be in the following states:

  1. Untracked: Newly created files
  2. Modified: Tracked files that have been changed
  3. Deleted: Tracked files that have been removed
bash
# Demo different states
git add README.md main.py src/utils.py
git commit -m "Initial commit"

# Modify tracked file
echo "Update content" >> README.md

# Create new file
echo "New feature" > feature.py

# Delete file
rm main.py

# Check status
git status

Output:

On branch 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:   README.md
        deleted:    main.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        feature.py

no changes added to commit (use "git add" or "git commit -a")

Staging Area (Index)

Definition and Characteristics

The Staging Area (also known as Index) is a buffer between the working directory and the repository, used to prepare content for the next commit.

Characteristics:

  • 🎯 Precise control over commit content
  • 📦 Can stage partial file changes
  • 🔄 Can modify staged content multiple times
  • 💾 Stored in .git/index file

Staging Area Operations

1. Add Files to Staging Area

bash
# Add single file
git add README.md

# Add multiple files
git add main.py feature.py

# Add directory
git add src/

# Add all changes
git add .

# Add all modifications to tracked files
git add -u

# Interactive add
git add -i

2. View Staging Area Content

bash
# Check staging area status
git status

# View diff between staging and working directory
git diff

# View diff between staging and last commit
git diff --staged
# Or
git diff --cached

3. Remove Files from Staging Area

bash
# Remove file from staging (keep working dir changes)
git restore --staged README.md

# Or using old syntax
git reset HEAD README.md

# Remove all staged changes
git restore --staged .

4. Partial Staging

Git allows you to stage only parts of a file's changes:

bash
# Create file for demo
cat > example.py << EOF
def function1():
    print("Function 1")

def function2():
    print("Function 2")
EOF
# (Further steps would involve git add -p)

Content is for learning and research only.