Skip to content

Commit 2c12d3a

Browse files
committed
Merge branch 'je/doc-data-model' into seen
Add a new manual that describes the data model. * je/doc-data-model: SQUASH??? fix xml that does not validate doc: add an explanation of Git's data model
2 parents be11443 + d4b7960 commit 2c12d3a

File tree

4 files changed

+300
-2
lines changed

4 files changed

+300
-2
lines changed

Documentation/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ MAN7_TXT += gitcli.adoc
5353
MAN7_TXT += gitcore-tutorial.adoc
5454
MAN7_TXT += gitcredentials.adoc
5555
MAN7_TXT += gitcvs-migration.adoc
56+
MAN7_TXT += gitdatamodel.adoc
5657
MAN7_TXT += gitdiffcore.adoc
5758
MAN7_TXT += giteveryday.adoc
5859
MAN7_TXT += gitfaq.adoc

Documentation/gitdatamodel.adoc

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
gitdatamodel(7)
2+
===============
3+
4+
NAME
5+
----
6+
gitdatamodel - Git's core data model
7+
8+
SYNOPSIS
9+
--------
10+
gitdatamodel
11+
12+
DESCRIPTION
13+
-----------
14+
15+
It's not necessary to understand Git's data model to use Git, but it's
16+
very helpful when reading Git's documentation so that you know what it
17+
means when the documentation says "object", "reference" or "index".
18+
19+
Git's core operations use 4 kinds of data:
20+
21+
1. <<object,Objects>>: commits, trees, blobs, and tag objects
22+
2. <<references,References>>: branches, tags,
23+
remote-tracking branches, etc
24+
3. <<index,The index>>, also known as the staging area
25+
4. <<reflogs,Reflogs>>: logs of changes to references ("ref log")
26+
27+
[[object]]
28+
OBJECTS
29+
-------
30+
31+
All of the commits and files in a Git repository are stored as "Git objects".
32+
Git objects never change after they're created, and every object has an ID,
33+
like `1b61de420a21a2f1aaef93e38ecd0e45e8bc9f0a`.
34+
35+
This means that if you have an object's ID, you can always recover its
36+
exact contents as long as the object hasn't been deleted.
37+
38+
Every object has:
39+
40+
[[object-id]]
41+
1. an *ID* (aka "object name"), which is a cryptographic hash of its
42+
type and contents.
43+
It's fast to look up a Git object using its ID.
44+
This is usually represented in hexadecimal, like
45+
`1b61de420a21a2f1aaef93e38ecd0e45e8bc9f0a`.
46+
2. a *type*. There are 4 types of objects:
47+
<<commit,commits>>, <<tree,trees>>, <<blob,blobs>>,
48+
and <<tag-object,tag objects>>.
49+
3. *contents*. The structure of the contents depends on the type.
50+
51+
Here's how each type of object is structured:
52+
53+
[[commit]]
54+
commit::
55+
A commit contains these required fields
56+
(though there are other optional fields):
57+
+
58+
1. The full directory structure of all the files in that version of the
59+
repository and each file's contents, stored as the *<<tree,tree>>* ID
60+
of the commit's base directory.
61+
2. Its *parent commit ID(s)*. The first commit in a repository has 0 parents,
62+
regular commits have 1 parent, merge commits have 2 or more parents
63+
3. An *author* and the time the commit was authored
64+
4. A *committer* and the time the commit was committed.
65+
5. A *commit message*
66+
+
67+
Here's how an example commit is stored:
68+
+
69+
----
70+
tree 1b61de420a21a2f1aaef93e38ecd0e45e8bc9f0a
71+
parent 4ccb6d7b8869a86aae2e84c56523f8705b50c647
72+
author Maya <maya@example.com> 1759173425 -0400
73+
committer Maya <maya@example.com> 1759173425 -0400
74+
75+
Add README
76+
----
77+
+
78+
Like all other objects, commits can never be changed after they're created.
79+
For example, "amending" a commit with `git commit --amend` creates a new
80+
commit with the same parent.
81+
+
82+
Git does not store the diff for a commit: when you ask Git to show
83+
the commit with linkgit:git-show[1], it calculates the diff from its
84+
parent on the fly.
85+
86+
[[tree]]
87+
tree::
88+
A tree is how Git represents a directory.
89+
It can contain files or other trees (which are subdirectories).
90+
It lists, for each item in the tree:
91+
+
92+
1. The *filename*, for example `hello.py`
93+
2. The *file mode*. Git has these file modes. which are only
94+
spiritually related to Unix file modes:
95+
+
96+
- `100644`: regular file (with <<object,object type>> `blob`)
97+
- `100755`: executable file (with type `blob`)
98+
- `120000`: symbolic link (with type `blob`)
99+
- `040000`: directory (with type `tree`)
100+
- `160000`: gitlink, for use with submodules (with type `commit`)
101+
102+
3. The <<object-id,*object ID*>> with the contents of the file or directory
103+
+
104+
For example, this is how a tree containing one directory (`src`) and one file
105+
(`README.md`) is stored:
106+
+
107+
----
108+
100644 blob 8728a858d9d21a8c78488c8b4e70e531b659141f README.md
109+
040000 tree 89b1d2e0495f66d6929f4ff76ff1bb07fc41947d src
110+
----
111+
112+
[[blob]]
113+
blob::
114+
A blob object contains a file's contents.
115+
+
116+
When you make a commit, Git stores the full contents of each file that
117+
you changed as a blob.
118+
For example, if you have a commit that changes 2 files in a repository
119+
with 1000 files, that commit will create 2 new blobs, and use the
120+
previous blob ID for the other 998 files.
121+
This means that commits can use relatively little disk space even in a
122+
very large repository.
123+
124+
[[tag-object]]
125+
tag object::
126+
Tag objects contain these required fields
127+
(though there are other optional fields):
128+
+
129+
1. The object *ID* it references
130+
2. The object *type*
131+
3. The *tagger* and tag date
132+
4. A *tag message*, similar to a commit message
133+
134+
Here's how an example tag object is stored:
135+
136+
----
137+
object 750b4ead9c87ceb3ddb7a390e6c7074521797fb3
138+
type commit
139+
tag v1.0.0
140+
tagger Maya <maya@example.com> 1759927359 -0400
141+
142+
Release version 1.0.0
143+
----
144+
145+
NOTE: All of the examples in this section were generated with
146+
`git cat-file -p <object-id>`.
147+
148+
[[references]]
149+
REFERENCES
150+
----------
151+
152+
References are a way to give a name to a commit.
153+
It's easier to remember "the changes I'm working on are on the `turtle`
154+
branch" than "the changes are in commit bb69721404348e".
155+
Git often uses "ref" as shorthand for "reference".
156+
157+
References can either refer to:
158+
159+
1. An object ID, usually a <<commit,commit>> ID
160+
2. Another reference. This is called a "symbolic reference".
161+
162+
References are stored in a hierarchy, and Git handles references
163+
differently based on where they are in the hierarchy.
164+
Most references are under `refs/`. Here are the main types:
165+
166+
[[branch]]
167+
branches: `refs/heads/<name>`::
168+
A branch refers to a commit ID.
169+
That commit is the latest commit on the branch.
170+
+
171+
To get the history of commits on a branch, Git will start at the commit
172+
ID the branch references, and then look at the commit's parent(s),
173+
the parent's parent, etc.
174+
175+
[[tag]]
176+
tags: `refs/tags/<name>`::
177+
A tag refers to a commit ID, tag object ID, or other object ID.
178+
There are two types of tags:
179+
1. "Annotated tags", which reference a <<tag-object,tag object>> ID
180+
which contains a tag message
181+
2. "Lightweight tags", which reference a commit, blob, or tree ID
182+
directly
183+
+
184+
Even though branches and tags both refer to a commit ID, Git
185+
treats them very differently.
186+
Branches are expected to change over time: when you make a commit, Git
187+
will update your <<HEAD,current branch>> to point to the new commit.
188+
Tags are usually not changed after they're created.
189+
190+
[[HEAD]]
191+
HEAD: `HEAD`::
192+
`HEAD` is where Git stores your current <<branch,branch>>,
193+
if there is a current branch. `HEAD` can either be:
194+
+
195+
1. A symbolic reference to your current branch, for example `ref:
196+
refs/heads/main` if your current branch is `main`.
197+
2. A direct reference to a commit ID. In this case there is no current branch.
198+
This is called "detached HEAD state", see the DETACHED HEAD section
199+
of linkgit:git-checkout[1] for more.
200+
201+
[[remote-tracking-branch]]
202+
remote-tracking branches: `refs/remotes/<remote>/<branch>`::
203+
A remote-tracking branch refers to a commit ID.
204+
It's how Git stores the last-known state of a branch in a remote
205+
repository. `git fetch` updates remote-tracking branches. When
206+
`git status` says "you're up to date with origin/main", it's looking at
207+
this.
208+
+
209+
`refs/remotes/<remote>/HEAD` is a symbolic reference to the remote's
210+
default branch. This is the branch that `git clone` checks out by default.
211+
212+
[[other-refs]]
213+
Other references::
214+
Git tools may create references anywhere under `refs/`.
215+
For example, linkgit:git-stash[1], linkgit:git-bisect[1],
216+
and linkgit:git-notes[1] all create their own references
217+
in `refs/stash`, `refs/bisect`, etc.
218+
Third-party Git tools may also create their own references.
219+
+
220+
Git may also create references other than `HEAD` at the base of the
221+
hierarchy, like `ORIG_HEAD`.
222+
223+
NOTE: Git may delete objects that aren't "reachable" from any reference.
224+
An object is "reachable" if we can find it by following tags to whatever
225+
they tag, commits to their parents or trees, and trees to the trees or
226+
blobs that they contain.
227+
For example, if you amend a commit, with `git commit --amend`,
228+
the old commit will usually not be reachable, so it may be deleted eventually.
229+
Reachable objects will never be deleted.
230+
231+
[[index]]
232+
THE INDEX
233+
---------
234+
The index, also known as the "staging area", is a list of files and
235+
the contents of each file, stored as a <<blob,blob>>.
236+
You can add files to the index or update the contents of a file in the
237+
index with linkgit:git-add[1]. This is called "staging" the file for commit.
238+
239+
Unlike a <<tree,tree>>, the index is a flat list of files.
240+
When you commit, Git converts the list of files in the index to a
241+
directory <<tree,tree>> and uses that tree in the new <<commit,commit>>.
242+
243+
Each index entry has 4 fields:
244+
245+
1. The *file mode*, which must be one of:
246+
- `100644`: regular file (with <<object,object type>> `blob`)
247+
- `100755`: executable file (with type `blob`)
248+
- `120000`: symbolic link (with type `blob`)
249+
- `160000`: gitlink, for use with submodules (with type `commit`)
250+
2. The *<<blob,blob>>* ID of the file,
251+
or (rarely) the *<<commit,commit>>* ID of the submodule
252+
3. The *stage number*, either 0, 1, 2, or 3. This is normally 0, but if
253+
there's a merge conflict there can be multiple versions of the same
254+
filename in the index.
255+
4. The *file path*, for example `src/hello.py`
256+
257+
It's extremely uncommon to look at the index directly: normally you'd
258+
run `git status` to see a list of changes between the index and <<HEAD,HEAD>>.
259+
But you can use `git ls-files --stage` to see the index.
260+
Here's the output of `git ls-files --stage` in a repository with 2 files:
261+
262+
----
263+
100644 8728a858d9d21a8c78488c8b4e70e531b659141f 0 README.md
264+
100644 665c637a360874ce43bf74018768a96d2d4d219a 0 src/hello.py
265+
----
266+
267+
[[reflogs]]
268+
REFLOGS
269+
-------
270+
271+
Every time a branch, remote-tracking branch, or HEAD is updated, Git
272+
updates a log called a "reflog" for that <<references,reference>>.
273+
This means that if you make a mistake and "lose" a commit, you can
274+
generally recover the commit ID by running `git reflog <reference>`.
275+
276+
A reflog is a list of log entries. Each entry has:
277+
278+
1. The *commit ID*
279+
2. *Timestamp* when the change was made
280+
3. *Log message*, for example `pull: Fast-forward`
281+
282+
Reflogs only log changes made in your local repository.
283+
They are not shared with remotes.
284+
285+
You can view a reflog with `git reflog <reference>`.
286+
For example, here's the reflog for a `main` branch which has changed twice:
287+
288+
----
289+
$ git reflog main --date=iso --no-decorate
290+
750b4ea main@{2025-09-29 15:17:05 -0400}: commit: Add README
291+
4ccb6d7 main@{2025-09-29 15:16:48 -0400}: commit (initial): Initial commit
292+
----
293+
294+
GIT
295+
---
296+
Part of the linkgit:git[1] suite

Documentation/glossary-content.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ This commit is referred to as a "merge commit", or sometimes just a
297297
identified by its <<def_object_name,object name>>. The objects usually
298298
live in `$GIT_DIR/objects/`.
299299
300-
[[def_object_identifier]]object identifier (oid)::
301-
Synonym for <<def_object_name,object name>>.
300+
[[def_object_identifier]]object identifier, object ID, oid::
301+
Synonyms for <<def_object_name,object name>>.
302302
303303
[[def_object_name]]object name::
304304
The unique identifier of an <<def_object,object>>. The

Documentation/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ manpages = {
193193
'gitcore-tutorial.adoc' : 7,
194194
'gitcredentials.adoc' : 7,
195195
'gitcvs-migration.adoc' : 7,
196+
'gitdatamodel.adoc' : 7,
196197
'gitdiffcore.adoc' : 7,
197198
'giteveryday.adoc' : 7,
198199
'gitfaq.adoc' : 7,

0 commit comments

Comments
 (0)