{"id":30850,"date":"2022-07-24T14:44:57","date_gmt":"2022-07-24T14:44:57","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=30850"},"modified":"2022-12-23T05:48:01","modified_gmt":"2022-12-23T05:48:01","slug":"git-tutorial-objects-references-the-index","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/git-tutorial-objects-references-the-index\/","title":{"rendered":"Git Tutorial: Objects, References, The Index"},"content":{"rendered":"\n<p>To understand the core of Git internals, there are 3 things to we should know: objects, references, the index.<\/p>\n\n\n\n<p>I find this model is elegant. It fits well in a small diagram, as well as in my head.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"562\" height=\"393\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/big-picture.png\" alt=\"\" class=\"wp-image-30851\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/big-picture.png 562w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/big-picture-300x210.png 300w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Objects<\/strong><\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>All files that you commited into a Git repository, including the commit info are stored as&nbsp;<strong>objects<\/strong>&nbsp;in .git\/objects\/.<\/p>\n\n\n\n<p>An object is identified by a 40-character-long string \u2013 SHA1 hash of the object\u2019s content.<\/p>\n\n\n\n<p>There are&nbsp;<strong>4 types<\/strong>&nbsp;of objects:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>blob &#8211; stores file content.<\/li><li>tree &#8211; stores direcotry layouts and filenames.<\/li><li>commit &#8211; stores commit info and forms the Git commit graph.<\/li><li>tag &#8211; stores annotated tag.<\/li><\/ol>\n\n\n\n<p>The example will illustrate how these objects relate to each others.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>References<\/strong><\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>A branch, remote branch or a tag (also called lightweight tag) in Git, is just a&nbsp;<strong>pointer to an object<\/strong>, usually a commit object.<\/p>\n\n\n\n<p>They are stored as plain text files in .git\/refs\/.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Symbolic References<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Git has a special kind of reference, called symbolic reference. It doesn\u2019t point to an object directly. Instead, it&nbsp;<strong>points to another reference<\/strong>.<\/p>\n\n\n\n<p>For instance, .git\/HEAD is a symbolic reference. It points to the current branch you are working on.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>The Index<\/strong><\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>The index is a staging area, stored as a binary file in .git\/index.<\/p>\n\n\n\n<p>When git add a file, Git adds the file info to the index. When git commit, Git only commits what\u2019s listed in the index.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Examples<\/h1>\n\n\n\n<p>Let&#8217;s walkthrough a simple example, to create a Git repository, commit some files and see what happened behind the scene in .git directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Initialize New Repository<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>$ git init canai<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"271\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/init.png\" alt=\"\" class=\"wp-image-30852\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/init.png 402w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/init-300x202.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure>\n\n\n\n<p>What happened:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Empty .git\/objects\/ and .git\/refs\/ created.<\/li><li>No index file yet.<\/li><li>HEAD symbolic reference created. $ cat .git\/HEAD ref: refs\/heads\/master<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add New File<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2<\/td><td>$ echo &#8220;A roti canai project.&#8221; &gt;&gt; README $ git add README<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"271\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/new-file.png\" alt=\"\" class=\"wp-image-30853\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/new-file.png 402w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/new-file-300x202.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure>\n\n\n\n<p>What happened:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Index file created.<br>It has a SHA1 hash that points to a blob object.<\/li><li>Blob object created.<br>The content of README file is stored in this blob.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">First Commit<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2 3 4<\/td><td>$ git commit -m&#8217;first commit&#8217; [master (root-commit) d9976cf] first commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 README<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"271\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/first-commit.png\" alt=\"\" class=\"wp-image-30854\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/first-commit.png 402w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/first-commit-300x202.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure>\n\n\n\n<p>What happened:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Branch &#8216;master&#8217; reference created.<br>It points to the lastest commit object in &#8216;master&#8217; branch.<\/li><li>First commit object created. It points to the root tree object.<\/li><li>Tree object created.<br>This tree represents the &#8216;canai&#8217; directory.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add Modified File<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2<\/td><td>$ echo &#8220;Welcome everyone.&#8221; &gt;&gt; README $ git add README<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"271\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/modified-file.png\" alt=\"\" class=\"wp-image-30855\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/modified-file.png 402w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/modified-file-300x202.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure>\n\n\n\n<p>What happened:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Index file updated.<br>Notice it points to a new blob?<\/li><li>Blob object created.<br>The entire README content is stored as a new blob.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add File into Subdirectory<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2 3<\/td><td>$ mkdir doc $ echo &#8220;[[TBD]] manual toc&#8221; &gt;&gt; doc\/manual.txt $ git add doc<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"271\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/subdir.png\" alt=\"\" class=\"wp-image-30856\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/subdir.png 402w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/subdir-300x202.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure>\n\n\n\n<p>What happened:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Index file updated.<\/li><li>Blob object created.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Second Commit<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2 3 4<\/td><td>$ git commit -m&#8217;second commit&#8217; [master 556eaf3] second commit 2 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 doc\/manual.txt<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"271\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/second-commit.png\" alt=\"\" class=\"wp-image-30857\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/second-commit.png 402w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/second-commit-300x202.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure>\n\n\n\n<p>What happened:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Branch &#8216;master&#8217; reference updated.<br>It points to a lastest commit in this branch.<\/li><li>Second commit object created. Notice its \u2018parent\u2019 points to the first commit object. This forms a commit graph.<\/li><li>New root tree object created.<\/li><li>New subdir tree object created.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add Annotated Tag<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>$ git tag -a -m&#8217;this is annotated tag&#8217; v0.1 d9976<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"271\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/annotated-tag.png\" alt=\"\" class=\"wp-image-30858\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/annotated-tag.png 402w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/annotated-tag-300x202.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><figcaption>What happened:<br>Tag reference created.<br>It points to a commit object.<br>1 2<br>$ cat .git\/refs\/tags\/root-commit d9976cfe0430557885d162927dd70186d0f521e8<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>To understand the core of Git internals, there are 3 things to we should know: objects, references, the index. I find this model is elegant. It fits well in a&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-30850","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=30850"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30850\/revisions"}],"predecessor-version":[{"id":30859,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30850\/revisions\/30859"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=30850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=30850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=30850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}