|
| 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. <<objects,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 | +[[objects]] |
| 28 | +OBJECTS |
| 29 | +------- |
| 30 | + |
| 31 | +Commits, trees, blobs, and tag objects are all stored in Git's object database. |
| 32 | +Every object has: |
| 33 | + |
| 34 | +[[object-id]] |
| 35 | +1. an *ID* (aka "object name"), which is a cryptographic hash of its |
| 36 | + type and contents. |
| 37 | + It's fast to look up a Git object using its ID. |
| 38 | + This is usually represented in hexadecimal, like |
| 39 | + `1b61de420a21a2f1aaef93e38ecd0e45e8bc9f0a`. |
| 40 | +2. a *type*. There are 4 types of objects: |
| 41 | + <<commit,commits>>, <<tree,trees>>, <<blob,blobs>>, |
| 42 | + and <<tag-object,tag objects>>. |
| 43 | +3. *contents*. The structure of the contents depends on the type. |
| 44 | +
|
| 45 | +Once an object is created, it can never be changed. |
| 46 | +Here are the 4 types of objects: |
| 47 | + |
| 48 | +[[commit]] |
| 49 | +commits:: |
| 50 | + A commit contains these required fields |
| 51 | + (though there are other optional fields): |
| 52 | ++ |
| 53 | +1. All the *files* in the commit, stored as the *<<tree,tree>>* ID of |
| 54 | + the commit's base directory. |
| 55 | +2. Its *parent commit ID(s)*. The first commit in a repository has 0 parents, |
| 56 | + regular commits have 1 parent, merge commits have 2 or more parents |
| 57 | +3. An *author* and the time the commit was authored |
| 58 | +4. A *committer* and the time the commit was committed. |
| 59 | + If you cherry-pick (linkgit:git-cherry-pick[1]) someone else's commit, |
| 60 | + then they will be the author and you'll be the committer. |
| 61 | +5. A *commit message* |
| 62 | ++ |
| 63 | +Here's how an example commit is stored: |
| 64 | ++ |
| 65 | +---- |
| 66 | +tree 1b61de420a21a2f1aaef93e38ecd0e45e8bc9f0a |
| 67 | +parent 4ccb6d7b8869a86aae2e84c56523f8705b50c647 |
| 68 | +author Maya <maya@example.com> 1759173425 -0400 |
| 69 | +committer Maya <maya@example.com> 1759173425 -0400 |
| 70 | + |
| 71 | +Add README |
| 72 | +---- |
| 73 | ++ |
| 74 | +Like all other objects, commits can never be changed after they're created. |
| 75 | +For example, "amending" a commit with `git commit --amend` creates a new |
| 76 | +commit with the same parent. |
| 77 | ++ |
| 78 | +Git does not store the diff for a commit: when you ask Git for a |
| 79 | +diff it calculates it on the fly. |
| 80 | + |
| 81 | +[[tree]] |
| 82 | +trees:: |
| 83 | + A tree is how Git represents a directory. It lists, for each item in |
| 84 | + the tree: |
| 85 | ++ |
| 86 | +[[file-mode]] |
| 87 | +1. The *file mode*, for example `100644`. The format is inspired by Unix |
| 88 | + permissions, but Git's modes are much more limited. Git only supports these file modes: |
| 89 | ++ |
| 90 | + - `100644`: regular file (with type `blob`) |
| 91 | + - `100755`: executable file (with type `blob`) |
| 92 | + - `120000`: symbolic link (with type `blob`) |
| 93 | + - `040000`: directory (with type `tree`) |
| 94 | + - `160000`: gitlink, for use with submodules (with type `commit`) |
| 95 | + |
| 96 | +2. The *type*: either <<blob,`blob`>> (a file), `tree` (a directory), |
| 97 | + or <<commit,`commit`>> (a Git submodule, which is a |
| 98 | + commit from a different Git repository) |
| 99 | +3. The <<object-id,*object ID*>> |
| 100 | +4. The *filename* |
| 101 | ++ |
| 102 | +For example, this is how a tree containing one directory (`src`) and one file |
| 103 | +(`README.md`) is stored: |
| 104 | ++ |
| 105 | +---- |
| 106 | +100644 blob 8728a858d9d21a8c78488c8b4e70e531b659141f README.md |
| 107 | +040000 tree 89b1d2e0495f66d6929f4ff76ff1bb07fc41947d src |
| 108 | +---- |
| 109 | +
|
| 110 | +
|
| 111 | +[[blob]] |
| 112 | +blobs:: |
| 113 | + A blob is how Git represents a file. A blob object contains the |
| 114 | + file's contents. |
| 115 | ++ |
| 116 | +When you make a new commit, Git only needs to store new versions of |
| 117 | +files which were changed in that commit. This means that commits |
| 118 | +can use relatively little disk space even in a very large repository. |
| 119 | + |
| 120 | +[[tag-object]] |
| 121 | +tag objects:: |
| 122 | + Tag objects contain these required fields |
| 123 | + (though there are other optional fields): |
| 124 | ++ |
| 125 | +1. The *ID* and *type* of the object (often a commit) that they reference |
| 126 | +2. The *tagger* and tag date |
| 127 | +3. A *tag message*, similar to a commit message |
| 128 | + |
| 129 | +Here's how an example tag object is stored: |
| 130 | + |
| 131 | +---- |
| 132 | +object 750b4ead9c87ceb3ddb7a390e6c7074521797fb3 |
| 133 | +type commit |
| 134 | +tag v1.0.0 |
| 135 | +tagger Maya <maya@example.com> 1759927359 -0400 |
| 136 | +
|
| 137 | +Release version 1.0.0 |
| 138 | +---- |
| 139 | + |
| 140 | +NOTE: All of the examples in this section were generated with |
| 141 | +`git cat-file -p <object-id>`, which shows the contents of a Git object. |
| 142 | + |
| 143 | +[[references]] |
| 144 | +REFERENCES |
| 145 | +---------- |
| 146 | + |
| 147 | +References are a way to give a name to a commit. |
| 148 | +It's easier to remember "the changes I'm working on are on the `turtle` |
| 149 | +branch" than "the changes are in commit bb69721404348e". |
| 150 | +Git often uses "ref" as shorthand for "reference". |
| 151 | + |
| 152 | +References can either be: |
| 153 | + |
| 154 | +1. References to an object ID, usually a <<commit,commit>> ID |
| 155 | +2. References to another reference. This is called a "symbolic reference". |
| 156 | +
|
| 157 | +References are stored in a hierarchy, and Git handles references |
| 158 | +differently based on where they are in the hierarchy. |
| 159 | +Most references are under `refs/`. Here are the main types: |
| 160 | + |
| 161 | +[[branch]] |
| 162 | +branches: `refs/heads/<name>`:: |
| 163 | + A branch is a name for a commit ID. |
| 164 | + That commit is the latest commit on the branch. |
| 165 | ++ |
| 166 | +To get the history of commits on a branch, Git will start at the commit |
| 167 | +ID the branch references, and then look at the commit's parent(s), |
| 168 | +the parent's parent, etc. |
| 169 | + |
| 170 | +[[tag]] |
| 171 | +tags: `refs/tags/<name>`:: |
| 172 | + A tag is a name for a commit ID, tag object ID, or other object ID. |
| 173 | + Tags that reference a tag object ID are called "annotated tags", |
| 174 | + because the tag object contains a tag message. |
| 175 | + Tags that reference a commit, blob, or tree ID are |
| 176 | + called "lightweight tags". |
| 177 | ++ |
| 178 | +Even though branches and tags are both "a name for a commit ID", Git |
| 179 | +treats them very differently. |
| 180 | +Branches are expected to change over time: when you make a commit, Git |
| 181 | +will update your <<HEAD,current branch>> to reference the new changes. |
| 182 | +Tags are usually not changed after they're created. |
| 183 | + |
| 184 | +[[HEAD]] |
| 185 | +HEAD: `HEAD`:: |
| 186 | + `HEAD` is where Git stores your current <<branch,branch>>. |
| 187 | + `HEAD` can either be: |
| 188 | + 1. A symbolic reference to your current branch, for example `ref: |
| 189 | + refs/heads/main` if your current branch is `main`. |
| 190 | + 2. A direct reference to a commit ID. This is called "detached HEAD |
| 191 | + state", see the DETACHED HEAD section of linkgit:git-checkout[1] for more. |
| 192 | + |
| 193 | +[[remote-tracking-branch]] |
| 194 | +remote tracking branches: `refs/remotes/<remote>/<branch>`:: |
| 195 | + A remote-tracking branch is a name for a commit ID. |
| 196 | + It's how Git stores the last-known state of a branch in a remote |
| 197 | + repository. `git fetch` updates remote-tracking branches. When |
| 198 | + `git status` says "you're up to date with origin/main", it's looking at |
| 199 | + this. |
| 200 | ++ |
| 201 | +`refs/remotes/<remote>/HEAD` is a symbolic reference to the remote's |
| 202 | +default branch. This is the branch that `git clone` checks out by default. |
| 203 | + |
| 204 | +[[other-refs]] |
| 205 | +Other references:: |
| 206 | + Git tools may create references anywhere under `refs/`. |
| 207 | + For example, linkgit:git-stash[1], linkgit:git-bisect[1], |
| 208 | + and linkgit:git-notes[1] all create their own references |
| 209 | + in `refs/stash`, `refs/bisect`, etc. |
| 210 | + Third-party Git tools may also create their own references. |
| 211 | ++ |
| 212 | +Git may also create references other than `HEAD` at the base of the |
| 213 | +hierarchy, like `ORIG_HEAD`. |
| 214 | ++ |
| 215 | +NOTE: By default, Git references are stored as files in the `.git` directory. |
| 216 | +For example, the branch `main` is stored in `.git/refs/heads/main`. |
| 217 | +This means that you can't have branches named both `maya` and `maya/some-task`, |
| 218 | +because there can't be a file and a directory with the same name. |
| 219 | + |
| 220 | +[[index]] |
| 221 | +THE INDEX |
| 222 | +--------- |
| 223 | + |
| 224 | +The index, also known as the "staging area", contains a list of every |
| 225 | +file in the repository and its contents. When you commit, the files in |
| 226 | +the index are used as the files in the next commit. |
| 227 | + |
| 228 | +You can add files to the index or update the version in the index with |
| 229 | +linkgit:git-add[1]. Adding a file to the index or updating its version |
| 230 | +is called "staging" the file for commit. |
| 231 | + |
| 232 | +Unlike a <<tree,tree>>, the index is a flat list of files. |
| 233 | +Each index entry has 4 fields: |
| 234 | + |
| 235 | +1. The *<<file-mode,file mode>>* |
| 236 | +2. The *<<blob,blob>> ID* of the file |
| 237 | +3. The *file path*, for example `src/hello.py` |
| 238 | +4. The *stage number*, either 0, 1, 2, or 3. This is normally 0, but if |
| 239 | + there's a merge conflict there can be multiple versions of the same |
| 240 | + filename in the index. |
| 241 | +
|
| 242 | +It's extremely uncommon to look at the index directly: normally you'd |
| 243 | +run `git status` to see a list of changes between the index and <<HEAD,HEAD>>. |
| 244 | +But you can use `git ls-files --stage` to see the index. |
| 245 | +Here's the output of `git ls-files --stage` in a repository with 2 files: |
| 246 | + |
| 247 | +---- |
| 248 | +100644 8728a858d9d21a8c78488c8b4e70e531b659141f 0 README.md |
| 249 | +100644 665c637a360874ce43bf74018768a96d2d4d219a 0 src/hello.py |
| 250 | +---- |
| 251 | + |
| 252 | +[[reflogs]] |
| 253 | +REFLOGS |
| 254 | +------- |
| 255 | + |
| 256 | +Every time a branch, remote-tracking branch, or HEAD is updated, Git |
| 257 | +updates a log called a "reflog" for that <<reference,reference>>. |
| 258 | +This means that if you make a mistake and "lose" a commit, you can |
| 259 | +generally recover the commit ID by running `git reflog <reference>`. |
| 260 | + |
| 261 | +Each reflog entry has: |
| 262 | + |
| 263 | +1. Before/after *commit IDs* |
| 264 | +2. *User* who made the change, for example `Maya <maya@example.com>` |
| 265 | +3. *Timestamp* when the change was made |
| 266 | +4. *Log message*, for example `pull: Fast-forward` |
| 267 | +
|
| 268 | +Reflogs only log changes made in your local repository. |
| 269 | +They are not shared with remotes. |
| 270 | + |
| 271 | +For example, here's how the reflog for `HEAD` in a repository with 2 |
| 272 | +commits is stored: |
| 273 | + |
| 274 | +---- |
| 275 | +0000000000000000000000000000000000000000 4ccb6d7b8869a86aae2e84c56523f8705b50c647 Maya <maya@example.com> 1759173408 -0400 commit (initial): Initial commit |
| 276 | +4ccb6d7b8869a86aae2e84c56523f8705b50c647 750b4ead9c87ceb3ddb7a390e6c7074521797fb3 Maya <maya@example.com> 1759173425 -0400 commit: Add README |
| 277 | +---- |
| 278 | + |
| 279 | +GIT |
| 280 | +--- |
| 281 | +Part of the linkgit:git[1] suite |
0 commit comments