tech katta

How Git thinks: snapshots, the working tree, the staging area and the repository

Lesson 13 of 23 · 5 min readFree

In this lesson

Build the mental model that makes every Git command make sense: snapshots instead of diffs, and the three places a change can live.

On this page
  1. Why this matters
  2. Git stores snapshots
  3. Three places a change can live
  4. Watching it happen
  5. Comparing the three places
  6. Moving changes backwards
  7. Setting your identity
  8. Try it

Most Git confusion comes from using commands before understanding the model underneath. Spend this lesson on the model. The next nine Git lessons are mostly applications of it.

Why this matters#

Git is how a team shares code, and on a server it is often how code gets deployed. If you treat Git as a mysterious save button, every mistake feels dangerous: you fear losing work, you avoid branches and you copy-paste commands you cannot explain. If you know where your changes are at each moment, you can predict what a command will do and fix mistakes calmly.

All the output in this lesson comes from a real repository created for the lesson with Git 2.43. Your commit hashes will differ from ours, which is normal.

Git stores snapshots#

Many people imagine Git storing a list of changes: "line 4 changed in file A". That is not how it works. Each time you make a commit, Git records a snapshot of your whole project at that moment. Files that did not change are not copied again; Git simply points to the same stored content. The result behaves like a series of full photographs, each linked to the one before it.

A commit contains four things: a pointer to the snapshot, a pointer to its parent commit (the one before it), the author with a time, and your message. Because each commit names its parent, commits form a chain, and that chain is your history. Showing "what changed" is done afterwards by comparing two snapshots.

Every commit gets an id, a 40-character hash computed from its contents, such as cbb2e11.... You can use the first 7 or so characters. If the content changes, the hash changes, which is how Git detects corruption and tells commits apart.

Three places a change can live#

This is the idea to hold on to. In a Git project, your files exist in three states at once:

  1. The working tree is the normal folder of files you see and edit.
  2. The staging area (also called the index) is a list of what will go into the next commit.
  3. The repository is the history of commits, stored in the hidden .git folder.

The usual flow is: edit files in the working tree, add the ones you want into the staging area, then commit to turn the staging area into a new snapshot in the repository. The staging area is the step that surprises beginners. It exists so you can choose exactly what goes into a commit, for instance two of five changed files, instead of committing everything.

Watching it happen#

Start a new project. git init creates the .git folder and nothing else.

Shell
git init
git status
Text
Initialized empty Git repository in /tmp/g13/demo/.git/
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Create two files and ask for a short status with git status -s:

Shell
echo 'PORT=5100' > config.txt
echo 'console.log("v1")' > server.js
git status -s
Text
?? config.txt
?? server.js

The two question marks mean untracked: the files exist in the working tree but Git has never been told about them. Stage one of them:

Shell
git add server.js
git status -s
Text
A  server.js
?? config.txt

The first column describes the staging area and the second describes the working tree. A means "added to staging, and unchanged since"; config.txt is still untracked. Now commit:

Shell
git commit -m "Add server"
git status -s
Text
[main (root-commit) d0ad6eb] Add server
 1 file changed, 1 insertion(+)
 create mode 100644 server.js
?? config.txt

The commit contains only server.js, because that was all we staged. config.txt stays behind in the working tree. If you wanted both, you would have staged both. Note that the working tree folder is just files; the history lives in .git.

Comparing the three places#

git diff compares two of these places, and which two depends on the flags. Edit server.js again and watch a change move through the areas:

Shell
echo 'console.log("v2")' >> server.js
git status -s
git diff
Text
 M server.js
?? config.txt
diff --git a/server.js b/server.js
index ae24b40..5c4b222 100644
--- a/server.js
+++ b/server.js
@@ -1 +1,2 @@
 console.log("v1")
+console.log("v2")

The leading space in M shows the change is in the working tree only. git diff with no flags compares the working tree to the staging area. Lines starting with + were added and - removed. Stage the file and compare again:

Shell
git add server.js
git diff
git diff --staged

The first git diff now prints nothing, because the working tree and the staging area match. git diff --staged prints the same difference as before, now comparing the staging area to the last commit. Status shows M (capital M in the first column).

Shell
git ls-files --stage
Text
100644 5c4b2228b188bd3d84c65e53fef64bdebaa5d86d 0	server.js

This lists what is in the staging area: the file mode, the hash of the content and the path. Commit it and look at the history:

Shell
git commit -m "Update server"
git log --oneline
Text
cbb2e11 Update server
d0ad6eb Add server

Two snapshots, newest first. You can look inside a commit:

Shell
git cat-file -p HEAD
git ls-tree HEAD
Text
tree 6664cf82811a15634cc887be845389d1f55864e5
parent d0ad6eb5f82b081c28ceea263cab79d1a3cd7abd

Update server
100644 blob 5c4b2228b188bd3d84c65e53fef64bdebaa5d86d	server.js

(We left out the author and committer lines.) You can see the pieces: the tree is the snapshot, parent points to the first commit, and the message follows. The ls-tree line shows the snapshot holds a blob, the stored content of server.js, with the same hash as the staging area had. Lesson 22 opens .git further.

Moving changes backwards#

The same three places explain how to undo work. Stage a change, then unstage it, then discard it:

Shell
echo 'x' >> server.js
git add server.js
git restore --staged server.js
git restore server.js

After git add, status shows M (staged). After git restore --staged, it shows M (modified, not staged), and the change is still in your file. After git restore server.js, the file is back to the last commit and the change is gone. That last step throws work away, because the working tree is the only place that change existed. Remember that rule: anything you committed is hard to lose, and anything uncommitted can be lost in one command.

Setting your identity#

Git records the author of each commit, so tell it who you are once per machine:

Shell
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Use the email you will use on your hosting service. Without these, Git may refuse to commit or invent a name from your machine's hostname. git config --list shows all current settings.

Try it#

  1. Create a folder, run git init and make two files. Use git status -s and write down what each column means for each file.
  2. Stage only one file, commit it, then confirm with git status -s that the other remains untracked.
  3. Edit the committed file. Compare git diff and git diff --staged before and after git add. Explain why each shows what it shows.
  4. Run git log --oneline, then git cat-file -p HEAD and find the tree and parent lines. Which commit has no parent?
  5. Make an edit, stage it, unstage it with git restore --staged, then discard it with git restore. Check git status -s after each step.