Git fundamentals

Working tree, staging area, and repository

Build the three-part mental model behind everyday Git commands.

8 minute lesson

~~~

Most everyday Git commands make sense when you picture three project states.

The working tree contains the files you see and edit.

The staging area, also called the index, holds the exact snapshot prepared for the next commit.

The repository stores completed commits. HEAD normally points to your current branch, and that branch points to its latest commit.

Suppose notes.txt matches the current commit. You edit it, then run:

git status
git diff

The working tree changed. The staging area and current commit did not.

Now stage the file:

git add notes.txt
git status
git diff --staged

git add copies the current file content into the index. It does not freeze the working file. If you edit notes.txt again, Git can show one staged version and another unstaged change at the same time.

Finally, commit the index:

git commit -m "Add project notes"
git status

Git creates a commit from the staged snapshot. The current branch reference moves to that commit. If the working tree still matches it, git status reports a clean state.

My advice is to ask this before every command: Which state will this command read, and which state will it change?

Exercise: stage a file, edit it again, and inspect both git diff and git diff --staged. Predict which version a commit would contain before you run it.

Lesson completed

Take this course offline

Get every free book, course edition, and software download.

Get the download library →