Git's model: snapshots, commits and pointers
Build an accurate picture of what Git stores: three places for your work, commits as snapshots of the whole project, and branches and HEAD as movable pointers.
Contentsand lesson path
On this page
- Why it matters
- Three places your work lives
- Commits are snapshots, not diffs
- Branches, tags and HEAD are pointers
- Check your understanding
- Apply it
- Review prompt
Lessons of Git Workflow
- Git's model: snapshots, commits and pointers Concept · 15 min
- Commit hygiene: staging hunks and writing useful messages Worked example · 15 min
- Branching, merging and resolving a real conflict Lab · 25 min
- Pull requests, reviews and short-lived branches Concept · 20 min
- Recovering from mistakes without breaking teammates Lab · 20 min
- Check: repository-state challenges Knowledge check · 8 questions
What you will learn
- Explain the working tree, staging area, commits and refs, and make small, focused commits with clear messages.
01. Why it matters
Whatever you build, whether model code, infrastructure definitions, an API or a web front end, your team's work meets in a Git repository. Most Git confusion does not come from forgetting a command. It comes from a fuzzy picture of what Git is storing, so a command does something perfectly logical that still surprises you.
This lesson builds that picture in three parts:
- the three places your work lives: the working tree, the staging area and the repository
- what a commit really is: a snapshot of the whole project plus a little metadata
- what branches, tags and
HEADare: names that point at commits
Once these are clear, the rest of the skill (branching, merging, pull requests and undoing mistakes) turns out to be a set of ways to create snapshots and move pointers.
02. Three places your work lives
Git keeps your project in three places, and most everyday commands copy content from one to another.
- The working tree is the ordinary folder you edit. Git watches it but never saves it on its own.
- The staging area, also called the index, is the draft of your next commit.
git addcopies the current version of a file into it. - The repository is the
.gitfolder.git committurns whatever is in the staging area into a permanent commit there.
The key consequence: a commit records what was staged, not what is on disk. In the session below, one line is added to notes.md and staged, then a second line is typed but not staged. git status lists the same file twice, once for each place it differs.
$ echo 'Cache the embeddings' >> notes.md
$ git add notes.md
$ echo 'Try a smaller model' >> notes.md
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: notes.md
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: notes.mdTwo diff commands show each gap. git diff --staged compares the staging area with the last commit, which is exactly what git commit would record. Plain git diff compares the working tree with the staging area: what you have changed but not yet staged.
$ git diff --staged
diff --git a/notes.md b/notes.md
index 17e0f0d..44626ab 100644
--- a/notes.md
+++ b/notes.md
@@ -1 +1,2 @@
# Notes
+Cache the embeddings
$ git diff
diff --git a/notes.md b/notes.md
index 44626ab..4b349e7 100644
--- a/notes.md
+++ b/notes.md
@@ -1,2 +1,3 @@
# Notes
Cache the embeddings
+Try a smaller modelIf you commit now, the commit contains the caching line only. git status --short afterwards still prints M notes.md, because the smaller-model line is waiting in the working tree. This is a feature rather than a trap: the staging area is how you choose exactly what goes into each commit, and the next lesson puts it to work.
03. Commits are snapshots, not diffs
Many people picture a commit as a list of changes. Git stores something different: every commit points to a complete snapshot of the project as it was staged. When you ask for a diff, Git works it out on the spot by comparing two snapshots.
Storing full snapshots sounds wasteful, but Git avoids duplication by naming every object after its content. Each object gets an ID that is a hash of its bytes, so identical content always gets the same ID and is stored only once. At an introductory level there are three kinds of object:
- a blob holds the contents of one file (just the bytes, not the name)
- a tree lists the names in one folder, pointing to a blob for each file and to another tree for each sub-folder
- a commit points to one top-level tree and records its parent commit (two for a merge), the author, the committer, the time and the message
You can inspect objects with git cat-file -p. Here is the commit just made, followed by the tree of that commit and the tree of its parent:
$ git cat-file -p HEAD
tree c3140925355d74c0541ceb1138b35947d914024c
parent ff8c1a6ae7ac9c52ab0fadcf0d152cd879a24fd8
author Ada Learner <[email protected]> 1792244904 +0000
committer Ada Learner <[email protected]> 1792244904 +0000
Record caching idea
$ git cat-file -p 'HEAD^{tree}'
100644 blob 11b15b1a4584b08fa423a57964bdbf018b0da0d5 app.py
100644 blob 44626ab5c55927789aa5c46470c5cd9a91e89de1 notes.md
$ git cat-file -p 'HEAD~1^{tree}'
100644 blob 11b15b1a4584b08fa423a57964bdbf018b0da0d5 app.py
100644 blob 17e0f0dedfdc83c924c6399a21434fc8240f488c notes.mdHEAD~1 means "the parent of HEAD". Read the two trees side by side: app.py did not change, so both snapshots point at the same blob, 11b15b1. Only notes.md has a new blob. A snapshot of a large project therefore costs little more than the files that changed. (Git also compresses objects and packs similar ones together, but that is a storage detail; the model you reason with is snapshots.)
Two practical consequences follow from content-based IDs:
- A commit ID covers the tree, the parents, the author, the time and the message. Change any of them, even by rewording an old message, and you get a different commit with a different ID. That is why commands that rewrite history, such as amending or rebasing, always produce new commits rather than editing old ones.
- If you create the same files with exactly the same bytes, your blob and tree IDs will normally match the ones above, but your commit IDs will not, because your name and clock differ. Throughout these lessons, expect your commit hashes to differ from the examples.
04. Branches, tags and HEAD are pointers
Commits link backwards: each one names its parent, so history is a chain (a graph, once branches merge) that you walk from newer to older. To find the newest commit of a line of work, Git uses refs, which are small named pointers.
- A branch such as
mainis a ref under.git/refs/heads/whose content is one commit ID. When you commit while on a branch, Git writes the new commit and moves that branch to it. - A tag such as
v1.0.0is a ref under.git/refs/tags/that is meant to stay where it was put. HEADrecords where you are. Normally it does not hold a commit ID at all: it names the current branch.
$ cat .git/HEAD
ref: refs/heads/main
$ git switch -c experiment
Switched to a new branch 'experiment'
$ echo 'print("hello, world")' > app.py
$ git commit -am 'Greet the whole world'
[experiment cd5dd22] Greet the whole world
1 file changed, 1 insertion(+), 1 deletion(-)
$ cat .git/HEAD
ref: refs/heads/experiment
$ cat .git/refs/heads/experiment
cd5dd220fb0e10cfdb46a172d5604c45e291958dCreating the branch copied no files: git switch -c experiment wrote one ref pointing at the commit you were on and made HEAD name it. The commit then moved only experiment. (In a fresh repository each ref is a small file like this; Git may later pack refs together, so in daily use ask Git with git rev-parse experiment instead of reading files.)
Back on main, one more commit makes the history fork. git log --oneline --graph --all draws the result, with ref names in brackets:
$ git switch main
Switched to branch 'main'
$ echo 'Ask about GPU budget' >> notes.md
$ git commit -am 'Record budget question'
[main bb6d675] Record budget question
1 file changed, 1 insertion(+)
$ git log --oneline --graph --all
* cd5dd22 (experiment) Greet the whole world
| * bb6d675 (HEAD -> main) Record budget question
|/
* 3502aaf Record smaller-model idea
* 9a665d2 Record caching idea
* ff8c1a6 Add notes and app main <- HEAD
|
v
ff8c1a6 <-- 9a665d2 <-- 3502aaf <-- bb6d675
^
|
cd5dd22 <-- experimentThe brackets appear when Git writes to a terminal; add --decorate if you pipe the output elsewhere.
HEAD can also point straight at a commit instead of a branch. This is called a detached HEAD, and you get one by asking for it, for example with git switch --detach <commit> to look at an old version. Commits made there belong to no branch, and Git warns you when you leave:
$ git switch --detach HEAD~1
HEAD is now at 3502aaf Record smaller-model idea
$ echo 'detached thought' >> notes.md
$ git commit -am 'Try an idea on a detached HEAD'
[detached HEAD c31e72f] Try an idea on a detached HEAD
1 file changed, 1 insertion(+)
$ git switch main
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
c31e72f Try an idea on a detached HEAD
If you want to keep it by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> c31e72f
Switched to branch 'main'Nothing is deleted at that moment. The commit still exists, but no ref leads to it, so it no longer appears in git log, and Git may clean it up after a grace period. If you want to keep it, follow the advice in the warning and give it a branch. A later lesson shows how to find commits like this again.
05. Check your understanding
06. Apply it
Use a repository you already work in, or a throwaway one created with git init -b main:
- Stage part of your work, change the same file again, and predict what
git status,git diffandgit diff --stagedwill show before you run them. - Run
git cat-file -p HEAD, then pass the tree ID it prints to anothergit cat-file -p. Find a file that did not change in the last commit and confirm that its blob ID matches the one in the parent commit's tree (git cat-file -p 'HEAD~1^{tree}'). - Run
git log --oneline --graph --alland sketch the graph on paper, marking where each branch andHEADpoint. Make one more commit, then note which pointer moved and which stayed put.
07. Review prompt
Without looking back, explain to a teammate in a few sentences: how the working tree, the staging area and the repository differ; why storing a snapshot per commit does not mean storing every file again; and what Git writes inside .git when you create a branch. Then check your explanation against the sections above.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.