How Git thinks: snapshots, the working tree, the staging area and the repository
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
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:
- The working tree is the normal folder of files you see and edit.
- The staging area (also called the index) is a list of what will go into the next commit.
- The repository is the history of commits, stored in the hidden
.gitfolder.
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.
git init
git statusInitialized 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:
echo 'PORT=5100' > config.txt
echo 'console.log("v1")' > server.js
git status -s?? config.txt
?? server.jsThe two question marks mean untracked: the files exist in the working tree but Git has never been told about them. Stage one of them:
git add server.js
git status -sA server.js
?? config.txtThe 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:
git commit -m "Add server"
git status -s[main (root-commit) d0ad6eb] Add server
1 file changed, 1 insertion(+)
create mode 100644 server.js
?? config.txtThe 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:
echo 'console.log("v2")' >> server.js
git status -s
git diff 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:
git add server.js
git diff
git diff --stagedThe 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).
git ls-files --stage100644 5c4b2228b188bd3d84c65e53fef64bdebaa5d86d 0 server.jsThis 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:
git commit -m "Update server"
git log --onelinecbb2e11 Update server
d0ad6eb Add serverTwo snapshots, newest first. You can look inside a commit:
git cat-file -p HEAD
git ls-tree HEADtree 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:
echo 'x' >> server.js
git add server.js
git restore --staged server.js
git restore server.jsAfter 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:
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#
- Create a folder, run
git initand make two files. Usegit status -sand write down what each column means for each file. - Stage only one file, commit it, then confirm with
git status -sthat the other remains untracked. - Edit the committed file. Compare
git diffandgit diff --stagedbefore and aftergit add. Explain why each shows what it shows. - Run
git log --oneline, thengit cat-file -p HEADand find thetreeandparentlines. Which commit has no parent? - Make an edit, stage it, unstage it with
git restore --staged, then discard it withgit restore. Checkgit status -safter each step.
Sign in to keep track of the lessons you finish.