{"id":30835,"date":"2022-07-24T14:32:28","date_gmt":"2022-07-24T14:32:28","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=30835"},"modified":"2022-12-23T05:48:03","modified_gmt":"2022-12-23T05:48:03","slug":"git-tutorial-types-of-git-objects","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/git-tutorial-types-of-git-objects\/","title":{"rendered":"Git Tutorial: Types of git objects"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">If<\/span> you have read Curious git, you know that git stores different types of objects in .git\/objects. The object types are:\r\n\r\ncommit;\r\ntree;\r\nblob;\r\nannotated tag.\r\nHere we make examples of each of these object types in a <span class=\"hljs-keyword\">new<\/span> repository.\r\n\r\nFirst we make the working tree <span class=\"hljs-keyword\">and<\/span> initialize the repository:\r\n\r\n$ mkdir example_repo\r\n$ cd example_repo\r\n$ git init\r\nInitialized <span class=\"hljs-keyword\">empty<\/span> Git repository in \/Users\/mb312\/dev_trees\/curious-git\/working\/example_repo\/.git\/\r\nNext we make an example commit:\r\n\r\n$ <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"An example file\"<\/span> &gt; example_file.txt\r\n$ git add example_file.txt\r\n$ git commit -m <span class=\"hljs-string\">\"An example commit\"<\/span>\r\n&#91;master (root-commit) cf1fdb2] An example commit\r\n <span class=\"hljs-number\">1<\/span> file changed, <span class=\"hljs-number\">1<\/span> insertion(+)\r\n create mode <span class=\"hljs-number\">100644<\/span> example_file.txt\r\nFrom Curious git, we expect there will now be three objects in the directory .git\/objects, one storing the backup of example_file.txt, one storing the directory listing <span class=\"hljs-keyword\">for<\/span> the commit, <span class=\"hljs-keyword\">and<\/span> one storing the commit message:\r\n\r\nobjects\r\n\u251c\u2500\u2500 <span class=\"hljs-number\">2<\/span>f\r\n\u2502   \u2514\u2500\u2500 <span class=\"hljs-number\">781156939<\/span>ad540b2434d012446154321e41e03 &#91;<span class=\"hljs-number\">32<\/span>B]\r\n\u251c\u2500\u2500 <span class=\"hljs-number\">83<\/span>\r\n\u2502   \u2514\u2500\u2500 <span class=\"hljs-number\">207<\/span>f0274383b4a79ff6d6c297e95204ba961bc &#91;<span class=\"hljs-number\">60<\/span>B]\r\n\u251c\u2500\u2500 cf\r\n\u2502   \u2514\u2500\u2500 <span class=\"hljs-number\">1<\/span>fdb215f948795523cde2987107944d1374777 &#91;<span class=\"hljs-number\">135<\/span>B]\r\n\u251c\u2500\u2500 info\r\n\u2514\u2500\u2500 pack\r\nCommit object type\r\nThe commit object contains the directory tree object hash, <span class=\"hljs-keyword\">parent<\/span> commit hash, author, committer, date <span class=\"hljs-keyword\">and<\/span> message.\r\n\r\nGit log will show us the hash <span class=\"hljs-keyword\">for<\/span> the commit message:\r\n\r\n$ git log\r\ncommit cf1fdb215f948795523cde2987107944d1374777\r\nAuthor: Matthew Brett \r\nDate:   Mon Feb <span class=\"hljs-number\">13<\/span> <span class=\"hljs-number\">16<\/span>:<span class=\"hljs-number\">56<\/span>:<span class=\"hljs-number\">24<\/span> <span class=\"hljs-number\">2017<\/span> +<span class=\"hljs-number\">0000<\/span>\r\n\r\n    An example commit\r\nNote\r\nI<span class=\"hljs-string\">'ll use git cat-file to show the contents of the hashed files in .git\/objects, but cat-file is a relatively obscure git command that you will probably not need in your daily git work.\r\n\r\ngit cat-file -t shows us the type of the object represented by a particular hash:\r\n\r\n$ git cat-file -t cf1fdb215f948795523cde2987107944d1374777\r\ncommit\r\ngit cat-file -p shows the contents of the file associated with this hash:\r\n\r\n$ git cat-file -p cf1fdb215f948795523cde2987107944d1374777\r\ntree 83207f0274383b4a79ff6d6c297e95204ba961bc\r\nauthor Matthew Brett  1487004984 +0000\r\ncommitter Matthew Brett  1487004984 +0000\r\n\r\nAn example commit\r\nTree object type\r\nThe commit contents gave us the hash of the directory listing for the commit. If we inspect this object, we find it is of type \u201ctree\u201d and contains the directory listing for the commit:\r\n\r\n$ git cat-file -t 83207f0274383b4a79ff6d6c297e95204ba961bc\r\ntree\r\n\r\n$ git cat-file -p 83207f0274383b4a79ff6d6c297e95204ba961bc\r\n100644 blob 2f781156939ad540b2434d012446154321e41e03\texample_file.txt\r\nThe tree object contains one line per file or subdirectory, with each line giving file permissions, object type, object hash and filename. Object type is usually one of \"blob\" for a file or \"tree\" for a subdirectory &#91;1].\r\n\r\nBlob object type\r\nThe directory listing gave us the hash of the stored of example_file.txt. This object is of type \u201cblob\u201d and contains the file snapshot:\r\n\r\n$ git cat-file -t 2f781156939ad540b2434d012446154321e41e03\r\nblob\r\n\r\n$ git cat-file -p 2f781156939ad540b2434d012446154321e41e03\r\nAn example file\r\nBlob is an abbreviation for \u201cbinary large object\u201d. When we git add a file such as example_file.txt, git creates a blob object containing the contents of the file. Blobs are therefore the git object type for storing files.\r\n\r\nTag object type\r\nThere is also a git type for annotated tags. We don\u2019t have one of those yet, so let\u2019s make one:\r\n\r\n$ git tag -a first-commit -m \"Tag pointing to first commit\"\r\nThis gives us a new object in .git\/objects:\r\n\r\nobjects\r\n\u251c\u2500\u2500 2f\r\n\u2502   \u2514\u2500\u2500 781156939ad540b2434d012446154321e41e03 &#91;32B]\r\n\u251c\u2500\u2500 83\r\n\u2502   \u2514\u2500\u2500 207f0274383b4a79ff6d6c297e95204ba961bc &#91;60B]\r\n\u251c\u2500\u2500 c7\r\n\u2502   \u2514\u2500\u2500 e94cc3a0d893a23dab7bb2f601dc8e682a42cc &#91;146B]\r\n\u251c\u2500\u2500 cf\r\n\u2502   \u2514\u2500\u2500 1fdb215f948795523cde2987107944d1374777 &#91;135B]\r\n\u251c\u2500\u2500 info\r\n\u2514\u2500\u2500 pack\r\nThe object is of type \"tag\":\r\n\r\n$ git cat-file -t c7e94cc3a0d893a23dab7bb2f601dc8e682a42cc\r\ntag\r\nThe tag object type contains the hash of the tagged object, the type of tagged object (usually a commit), the tag name, author, date and message:\r\n\r\n$ git cat-file -p c7e94cc3a0d893a23dab7bb2f601dc8e682a42cc\r\nobject cf1fdb215f948795523cde2987107944d1374777\r\ntype commit\r\ntag first-commit\r\ntagger Matthew Brett  1487004984 +0000\r\n\r\nTag pointing to first commit\r\nNotice that the \"object\" the tag points to, via its hash, is the commit object, as we were expecting.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>","protected":false},"excerpt":{"rendered":"","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-30835","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30835","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=30835"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30835\/revisions"}],"predecessor-version":[{"id":30836,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30835\/revisions\/30836"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=30835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=30835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=30835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}