linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/lessons/git-lab-05-1-three-areas

lesson ── git-labs ── ~18 мин ── 8 шагов

Three areas: working tree, index, repository

The goal is to see Git's three areas by hand and watch a file move between them. A file travels the path: working tree -> staging (index) -> repository. At every step you check git status and git diff with the right options to understand where each version sits right now.

▶ интерактивный sandbox

Поднимется контейнер gitlab/git-base с git, bash, pre-commit. В браузере откроется терминал, можно сразу git init. Каждый шаг проверяется автоматически. Сеть air-gapped, github.com недоступен.

запустить sandbox →

stack ── git · bash · 256 MB RAM · air-gapped · самоуничтожается через 30 мин простоя

Шаги

  1. 01

    Create a repo and the first commit

    bash
    cd /home/student/work
    mkdir -p three-areas && cd three-areas
    git init -b main
    echo "version 1" > note.txt
    git add note.txt
    git commit -m "init"
    git status

    Right now git status should say nothing to commit, working tree clean: all three areas match.

    ✓ Baseline: all three areas are identical.

  2. 02

    Change the file, look at the status

    bash
    cd /home/student/work/three-areas
    echo "version 2" > note.txt
    git status

    You see Changes not staged for commit. The file changed in the working tree, but the index still holds version 1. Check it:

    bash
    git diff           # without flags = working tree vs index
    git diff --cached  # --cached = index vs HEAD (empty, index = HEAD)
    подсказка

    If `git diff` is empty, you did not save the change to note.txt.

    ✓ Working tree differs from the index. Index = HEAD.

  3. 03

    Run git add: lift the change into the index

    bash
    cd /home/student/work/three-areas
    git add note.txt
    git status
    git diff           # empty: working = index
    git diff --cached  # sees the change: index != HEAD

    The change rose into the "staged" area. The working tree now matches the index, and the index differs from HEAD.

    ✓ The change is in the index. Working tree and index match.

  4. 04

    Build a situation where staged and unstaged hold different versions

    bash
    cd /home/student/work/three-areas
    echo "version 3" > note.txt
    git status
    git diff
    git diff --cached

    Now all three areas differ:

    • HEAD: version 1
    • index: version 2 (you staged it earlier)
    • working: version 3 (just written)

    This is normal during a long work session. git diff (without flags) shows v2 -> v3, while git diff --cached shows v1 -> v2.

    ✓ Three different versions in three areas. status shows both.

  5. 05

    Commit only what is in the index

    bash
    cd /home/student/work/three-areas
    git commit -m "bump to v2"     # commits the snapshot from the index, the working tree is ignored
    git status
    git log --oneline

    The commit was built from the index, not from the working tree. version 3 stayed in the working tree: nobody has recorded it yet. git status shows Changes not staged for commit.

    ✓ The index is committed; the working tree moved ahead. The areas split again.

  6. 06

    Roll back the commit with reset --soft

    --soft moves only the branch pointer. It does not touch the index or the working tree.

    bash
    cd /home/student/work/three-areas
    git add note.txt              # lift v3 into the index so there is something to show
    git commit -m "bump to v3"
    git log --oneline
    # --soft = move the branch to HEAD~1 (parent) only, leave index and working tree alone
    git reset --soft HEAD~1
    git log --oneline             # the last commit is gone from the log
    git status                    # but the index still holds the change

    The commit disappeared from the log, but version 3 is still in the index. Useful when you commit and immediately realize you want to add something else.

    ✓ The commit is removed; the index kept your work.

  7. 07

    Run reset --mixed: also reset the index

    --mixed (the default) rewinds the branch AND resets the index. It does not touch the working tree.

    bash
    cd /home/student/work/three-areas
    git reset --mixed HEAD        # --mixed (default) = reset index to HEAD, leave working tree alone
    git status                    # you see "Changes not staged"
    cat note.txt                  # the file still holds version 3

    The index is empty, the file in the working tree stays changed. This is the equivalent of "everything unstaged, but the text is still there".

    ✓ The index is reset. The working tree is untouched.

  8. 08

    Run reset --hard: wipe everything

    --hard is the maximum rollback: branch + index + working tree. Work in the working tree is lost without confirmation.

    bash
    cd /home/student/work/three-areas
    git reset --hard HEAD         # --hard = branch + index + working tree, all back to HEAD
    cat note.txt                  # the content of version 2 (from the last commit)
    git status                    # working tree clean

    note.txt holds version 2 again (from the last commit). Working tree clean.

    Rule: --hard is dangerous. Before you reach for it, think about whether you should first commit or stash.

    ✓ Everything is reset to the state of the last commit. The three areas match again.

Что ты узнал

Git's three areas: working tree (disk), index (staged), repository (commits). add lifts a change from the working tree into the index. commit records the index into the repo. reset --soft/--mixed/--hard are three levels of rollback, differing in which areas they touch.

команды

  • git statuscompare all three areas at a glance
  • git diffworking tree vs index
  • git diff --cachedindex vs HEAD
  • git reset --soft HEAD~1rewind only the branch pointer
  • git reset --mixed HEAD~1also reset the index
  • git reset --hard HEAD~1also reset the working tree

концепции

  • · the three areas live independently; status shows the differences between them
  • · diff without flags is working tree vs index
  • · diff --cached is index vs HEAD

← предыдущая

Reconstruct the log by hand with cat-file

следующая →

Branches: create, switch, merge, delete

Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies