Skip to content
GKgkml.dev
← Learn Git & GitHub

Lesson 2 of 7 · 8 min

The three trees: working directory, index, HEAD

Understand where a change lives at any moment, and every add, reset and checkout becomes obvious.

The three places a file exists

The working directory is what is on disk. The index — also called the staging area — is a proposed next commit. HEAD is the last commit on the current branch.

'Modified' means the working directory differs from the index. 'Staged' means the index differs from HEAD. That is the whole of git status, and it is why a file can be listed in both sections at once: some of its changes are staged and some are not.

Which command moves what
git add file        # working directory -> index
git commit          # index -> new commit, branch pointer advances

git restore file            # index -> working directory (discard edits)
git restore --staged file   # HEAD -> index (unstage, keep edits)

git diff            # working directory vs index (unstaged)
git diff --staged   # index vs HEAD (what would be committed)
git diff HEAD       # working directory vs HEAD (everything)

The three resets

CommandBranch pointerIndexWorking directory
reset --soft <c>movesunchangedunchanged
reset --mixed <c>movesreset to <c>unchanged (the default)
reset --hard <c>movesreset to <c>overwritten — edits lost

Squashing with soft reset

reset --soft is the clean way to combine the last few commits. Move the branch pointer back three commits and the index still holds all their content, staged and ready. One commit later, three have become one, with no interactive rebase involved.

The general rule: reset moves the branch pointer, switch and checkout move HEAD to a different branch. Confusing the two is the source of most reset accidents.

Worth remembering

  • add copies working directory → index; commit copies index → a new commit.
  • reset moves the branch pointer; checkout and switch move HEAD.
  • --soft, --mixed and --hard differ only in how far down they reach.

Test it now

Staging and reset semantics are the easy bank's most common subject.