lesson ── git-labs ── ~18 мин ── 8 шагов
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 недоступен.
stack ── git · bash · 256 MB RAM · air-gapped · самоуничтожается через 30 мин простоя
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.
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:
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.
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.
cd /home/student/work/three-areas
echo "version 3" > note.txt
git status
git diff
git diff --cached
Now all three areas differ:
version 1version 2 (you staged it earlier)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.
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.
--soft moves only the branch pointer. It does not touch the index or the working tree.
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.
--mixed (the default) rewinds the branch AND resets the index. It does not
touch the working tree.
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.
--hard is the maximum rollback: branch + index + working tree. Work in the
working tree is lost without confirmation.
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 glancegit diffworking tree vs indexgit diff --cachedindex vs HEADgit reset --soft HEAD~1rewind only the branch pointergit reset --mixed HEAD~1also reset the indexgit reset --hard HEAD~1also reset the working treeконцепции