interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / gitdatamodel.adoc

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