{"id":30860,"date":"2022-07-24T14:46:28","date_gmt":"2022-07-24T14:46:28","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=30860"},"modified":"2022-12-23T05:48:01","modified_gmt":"2022-12-23T05:48:01","slug":"git-tutorial-working-with-git-objects-using-git-cat-file","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/git-tutorial-working-with-git-objects-using-git-cat-file\/","title":{"rendered":"Git Tutorial: Working with git objects using &#8220;git cat-file&#8221;"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Git inside: Direct work with git objects<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Goals<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Explore the structure of the database objects<\/li><li>Using SHA1 hashes for searching the content in repository<\/li><\/ul>\n\n\n\n<p>Let us examine git objects with some tools.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><em>01.<\/em><strong>&nbsp;Searching for the last commit<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">RUN:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>git hist &#8211;max-count=1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This command should find the last commit in the repository. SHA1 hash is probably different on our systems; however you should see something like this.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">RESULT:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2<\/td><td>$ git hist &#8211;max-count=1 * 8029c07 2011-03-09 | Added index.html. (HEAD, master) [Alexander Shvets]<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><em>02.<\/em><strong>&nbsp;Display of the last commit<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>With SHA1 hash of a commit, as above&#8230;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">RUN:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2<\/td><td>git cat-file -t &lt;hash&gt; git cat-file -p &lt;hash&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>I see this &#8230;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">RESULT:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2 3 4 5 6 7 8 9<\/td><td>$ git cat-file -t 8029c07 commit $ git cat-file -p 8029c07 tree 096b74c56bfc6b40e754fc0725b8c70b2038b91e parent 567948ac55daa723807c0c16e34c76797efbcbed author Alexander Shvets &lt;alex@githowto.com&gt; 1299684476 -0500 committer Alexander Shvets &lt;alex@githowto.com&gt; 1299684476 -0500 Added index.html.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Note:<\/strong><br>If you specify the alias as \u00abtype\u00bb and \u00abdump\u00bb, as described in the corresponding lesson, you can enter commands git type and git dump instead of a long command (which I never memorize).<\/p>\n\n\n\n<p>This displays the commit object, which is in the head of master branch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><em>03.<\/em><strong>&nbsp;Tree search<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>We can display the tree referenced in the commit. This should be a file description (top level) in our project (for a specific commit). Use the SHA1 hash of the tree string from the list above.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">RUN:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>git cat-file -p &lt;treehash&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Here is my tree &#8230;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">RESULT:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2 3<\/td><td>$ git cat-file -p 096b74c 100644 blob 28e0e9d6ea7e25f35ec64a43f569b550e8386f90 index.html 040000 tree e46f374f5b36c6f02fb3e9e922b79044f754d795 lib<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>I can see the index.html file and lib folder.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><em>04.<\/em><strong>&nbsp;Display lib directory<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">RUN:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>git cat-file -p &lt;libhash&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">RESULT:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2<\/td><td>$ git cat-file -p e46f374 100644 blob c45f26b6fdc7db6ba779fc4c385d9d24fc12cf72 hello.html<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>There is a hello.html file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><em>05.<\/em><strong>&nbsp;Display hello.html file<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">RUN:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td>git cat-file -p &lt;hellohash&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">RESULT:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1 2 3 4 5 6 7 8 9<\/td><td>$ git cat-file -p c45f26b &lt;!&#8211; Author: Alexander Shvets (alex@githowto.com) &#8211;&gt; &lt;html&gt; &lt;head&gt; &lt;\/head&gt; &lt;body&gt; &lt;h1&gt;Hello, World!&lt;\/h1&gt; &lt;\/body&gt; &lt;\/html&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>And there it is. Tree objects, commit objects and blob objects are displayed directly from the git repository. That&#8217;s all there is &#8211; trees, blobs and commits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><em>06.<\/em><strong>&nbsp;Explore by yourself<\/strong><\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>The git repository can be explored manually. Try to manually find the original hello.html file from the first commit with help of SHA1 hash references in the last commit.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git inside: Direct work with git objects Goals Explore the structure of the database objects Using SHA1 hashes for searching the content in repository Let us examine git objects with&#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-30860","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30860","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=30860"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30860\/revisions"}],"predecessor-version":[{"id":30861,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30860\/revisions\/30861"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=30860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=30860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=30860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}