Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/Documentation/gitdatamodel.adoc # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / Documentation / gitdatamodel.adoc` 305 lines ยท 10656 bytes Raw Blame History 1 gitdatamodel(7) 2 =============== 3 4 NAME 5 ---- 6 gitdatamodel - Git's core data model 7 8 DESCRIPTION 9 ----------- 10 11 It's not necessary to understand Git's data model to use Git, but it's 12 very helpful when reading Git's documentation so that you know what it 13 means when the documentation says "object", "reference" or "index". 14 15 Git's core operations use 4 kinds of data: 16 17 1. <>: commits, trees, blobs, and tag objects 18 2. <>: branches, tags, 19 remote-tracking branches, etc 20 3. <>, also known as the staging area 21 4. <>: logs of changes to references ("ref log") 22 23 [[objects]] 24 OBJECTS 25 ------- 26 27 All of the commits and files in a Git repository are stored as "Git objects". 28 Git objects never change after they're created, and every object has an ID, 29 like ``1b61de420a21a2f1aaef93e38ecd0e45e8bc9f0a``. 30 31 This means that if you have an object's ID, you can always recover its 32 exact contents as long as the object hasn't been deleted. 33 34 Every object has: 35 36 [[object-id]] 37 1. 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``. 42 2. a *type*. There are 4 types of objects: 43 <>, <>, <>, 44 and <>. 45 3. *contents*. The structure of the contents depends on the type. 46 47 Here's how each type of object is structured: 48 49 [[commit]] 50 commit:: 51 A commit contains these required fields 52 (though there are other optional fields): 53 + 54 1. The full directory structure of all the files in that version of the 55 repository and each file's contents, stored as the *<>* ID 56 of the commit's top-level directory 57 2. 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 59 3. An *author* and the time the commit was authored 60 4. A *committer* and the time the commit was committed 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 `1759173425` -0400 69 committer Maya `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 to show 79 the commit with linkgit:git-show[1], it calculates the diff from its 80 parent on the fly. 81 82 [[tree]] 83 tree:: 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 + 88 1. The *filename*, for example `hello.py` 89 2. 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) 95 3. The <> with the contents of the file, directory, 96 or gitlink. 97 + 98 For example, this is how a tree containing one directory (`src`) and one file 99 (`README.md`) is stored: 100 + 101 ---- 102 100644 blob `8728a858d9d21a8c78488c8b4e70e531b659141f` README.md 103 040000 tree `89b1d2e0495f66d6929f4ff76ff1bb07fc41947d` src 104 ---- 105 106 NOTE: In the output above, Git displays the file type of each tree entry 107 using a format that's loosely modelled on Unix file modes (`100644` is 108 "regular file", `100755` is "executable file", `120000` is "symbolic 109 link", `040000` is "directory", and `160000` is "gitlink"). It also 110 displays the object's type: `blob` for files and symlinks, `tree` for 111 directories, and `commit` for gitlinks. 112 113 [[blob]] 114 blob:: 115 A blob object contains a file's contents. 116 + 117 When you make a commit, Git stores the full contents of each file that 118 you changed as a blob. 119 For example, if you have a commit that changes 2 files in a repository 120 with 1000 files, that commit will create 2 new blobs, and use the 121 previous blob ID for the other 998 files. 122 This means that commits can use relatively little disk space even in a 123 very large repository. 124 125 [[tag-object]] 126 tag object:: 127 Tag objects contain these required fields 128 (though there are other optional fields): 129 + 130 1. The *ID* of the object it references 131 2. The *type* of the object it references 132 3. The *tagger* and tag date 133 4. A *tag message*, similar to a commit message 134 135 Here's how an example tag object is stored: 136 137 ---- 138 object `750b4ead9c87ceb3ddb7a390e6c7074521797fb3` 139 type commit 140 tag v1.0.0 141 tagger Maya `1759927359` -0400 142 143 Release version 1.0.0 144 ---- 145 146 NOTE: All of the examples in this section were generated with 147 `git cat-file -p `. 148 149 [[references]] 150 REFERENCES 151 ---------- 152 153 References are a way to give a name to a commit. 154 It's easier to remember "the changes I'm working on are on the `turtle` 155 branch" than "the changes are in commit `bb69721404348e`". 156 Git often uses "ref" as shorthand for "reference". 157 158 References can either refer to: 159 160 1. An object ID, usually a <> ID 161 2. Another reference. This is called a "symbolic reference" 162 163 References are stored in a hierarchy, and Git handles references 164 differently based on where they are in the hierarchy. 165 Most references are under `refs/`. Here are the main types: 166 167 [[branch]] 168 branches: `refs/heads/`:: 169 A branch refers to a commit ID. 170 That commit is the latest commit on the branch. 171 + 172 To get the history of commits on a branch, Git will start at the commit 173 ID the branch references, and then look at the commit's parent(s), 174 the parent's parent, etc. 175 176 [[tag]] 177 tags: `refs/tags/`:: 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 <> ID 181 which contains a tag message 182 2. "Lightweight tags", which reference a commit, blob, or tree ID 183 directly 184 + 185 Even though branches and tags both refer to a commit ID, Git 186 treats them very differently. 187 Branches are expected to change over time: when you make a commit, Git 188 will update your <> to point to the new commit. 189 Tags are usually not changed after they're created. 190 191 [[HEAD]] 192 HEAD: `HEAD`:: 193 `HEAD` is where Git stores your current <>, 194 if there is a current branch. `HEAD` can either be: 195 + 196 1. A symbolic reference to your current branch, for example `ref: 197 refs/heads/main` if your current branch is `main`. 198 2. 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]] 203 remote-tracking branches: `refs/remotes//`:: 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//HEAD` is a symbolic reference to the remote's 211 default branch. This is the branch that `git clone` checks out by default. 212 213 [[other-refs]] 214 Other 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 + 221 Git may also create references other than `HEAD` at the base of the 222 hierarchy, like `ORIG_HEAD`. 223 224 NOTE: Git may delete objects that aren't "reachable" from any reference 225 or <>. 226 An object is "reachable" if we can find it by following tags to whatever 227 they tag, commits to their parents or trees, and trees to the trees or 228 blobs that they contain. 229 For example, if you amend a commit with `git commit --amend`, 230 there will no longer be a branch that points at the old commit. 231 The old commit is recorded in the current branch's <>, 232 so it is still "reachable", but when the reflog entry expires it may 233 become unreachable and get deleted. 234 Reachable objects will never be deleted. 235 236 [[index]] 237 THE INDEX 238 --------- 239 The index, also known as the "staging area", is a list of files and 240 the contents of each file, stored as a <>. 241 You can add files to the index or update the contents of a file in the 242 index with linkgit:git-add[1]. This is called "staging" the file for commit. 243 244 Unlike a <>, the index is a flat list of files. 245 When you commit, Git converts the list of files in the index to a 246 directory <> and uses that tree in the new <>. 247 248 Each index entry has 4 fields: 249 250 1. The *file type*, which must be one of: 251 - *regular file* 252 - *executable file* 253 - *symbolic link* 254 - *gitlink* (for use with submodules) 255 2. The *<>* ID of the file, 256 or (rarely) the *<>* ID of the submodule 257 3. 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. 260 4. The *file path*, for example `src/hello.py` 261 262 It's extremely uncommon to look at the index directly: normally you'd 263 run `git status` to see a list of changes between the index and <>. 264 But you can use `git ls-files --stage` to see the index. 265 Here's the output of `git ls-files --stage` in a repository with 2 files: 266 267 ---- 268 100644 `8728a858d9d21a8c78488c8b4e70e531b659141f` 0 README.md 269 100644 `665c637a360874ce43bf74018768a96d2d4d219a` 0 src/hello.py 270 ---- 271 272 [[reflogs]] 273 REFLOGS 274 ------- 275 276 Every time a branch, remote-tracking branch, or HEAD is updated, Git 277 updates a log called a "reflog" for that <>. 278 This means that if you make a mistake and "lose" a commit, you can 279 generally recover the commit ID by running `git reflog `. 280 281 A reflog is a list of log entries. Each entry has: 282 283 1. The *commit ID* 284 2. *Timestamp* when the change was made 285 3. *Log message*, for example `pull: Fast-forward` 286 287 Reflogs only log changes made in your local repository. 288 They are not shared with remotes. 289 290 You can view a reflog with `git reflog `. 291 For example, here's the reflog for a `main` branch which has changed twice: 292 293 ---- 294 $ git reflog main --date=iso --no-decorate 295 `750b4ea` main@{2025-09-29 15:17:05 -0400}: commit: Add README 296 `4ccb6d7` main@{2025-09-29 15:16:48 -0400}: commit (initial): Initial commit 297 ---- 298 299 SEE ALSO 300 -------- 301 linkgit:gitglossary[7] 302 303 GIT 304 --- 305 Part of the linkgit:git[1] suite