{"id":30833,"date":"2022-07-24T14:30:29","date_gmt":"2022-07-24T14:30:29","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=30833"},"modified":"2022-12-23T05:48:03","modified_gmt":"2022-12-23T05:48:03","slug":"git-tutorial-creating-blobs-tree-and-commits","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/git-tutorial-creating-blobs-tree-and-commits\/","title":{"rendered":"Git Tutorial: Creating Blobs Tree and Commits"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Creating Blobs<br>Creating a blob in your Git repository and getting a SHA back is pretty easy. The git hash-object command is all you&#8217;ll need. To create a blob object from an existing file, just run it with the &#8216;-w&#8217; option (which tells it to write the blob, not just compute the SHA).<\/p>\n\n\n\n<p>$ git hash-object -w myfile.txt<br>6ff87c4664981e4397625791c8ea3bbb5f2279a3<\/p>\n\n\n\n<p>$ git hash-object -w myfile2.txt<br>3bb0e8592a41ae3185ee32266c860714980dbed7<br>The STDOUT output of the command will the the SHA of the blob that was created.<\/p>\n\n\n\n<p>Creating Trees<br>Now lets say you want to create a tree from your new objects. The git mktree command makes it pretty simple to generate new tree objects from git ls-tree formatted output. For example, if you write the following to a file named &#8216;\/tmp\/tree.txt&#8217; :<\/p>\n\n\n\n<p>100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3 file1<br>100644 blob 3bb0e8592a41ae3185ee32266c860714980dbed7 file2<br>and then piped that through the git mktree command, Git will write a new tree to the object database and give you back the new sha of that tree.<\/p>\n\n\n\n<p>$ cat \/tmp\/tree.txt | git mk-tree<br>f66a66ab6a7bfe86d52a66516ace212efa00fe1f<br>Then, we can take that and make it a subdirectory of yet another tree, and so on. If we wanted to create a new tree with that one as a subtree, we just create a new file (\/tmp\/newtree.txt) with our new SHA as a tree in it:<\/p>\n\n\n\n<p>100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3 file1-copy<br>040000 tree f66a66ab6a7bfe86d52a66516ace212efa00fe1f our_files<br>and then use git mk-tree again:<\/p>\n\n\n\n<p>$ cat \/tmp\/newtree.txt | git mk-tree<br>5bac6559179bd543a024d6d187692343e2d8ae83<br>And we now have an artificial directory structure in Git that looks like this:<\/p>\n\n\n\n<p>|&#8211; file1-copy<br><code>-- our_files |-- file1 <\/code>&#8212; file2<\/p>\n\n\n\n<p>1 directory, 3 files<br>without that structure ever having actually existed on disk. Plus, we have a SHA (5bac6559) that points to it.<\/p>\n\n\n\n<p>Rearranging Trees<br>We can also do tree manipulation by combining trees into new structures using the index file. As a simple example, let&#8217;s take the tree we just created and make a new tree that has two copies of our 5bac6559 tree in it using a temporary index file. (You can do this by resetting the GIT_INDEX_FILE environment variable or on the command line)<\/p>\n\n\n\n<p>First, we read the tree into our index file under a new prefix using the git read-tree command, and then write the index contents as a tree using the git write-tree command:<\/p>\n\n\n\n<p>$ export GIT_INDEX_FILE=\/tmp\/index<br>$ git read-tree &#8211;prefix=copy1\/ 5bac6559<br>$ git read-tree &#8211;prefix=copy2\/ 5bac6559<br>$ git write-tree<br>bb2fa6de7625322322382215d9ea78cfe76508c1<\/p>\n\n\n\n<p>$&gt;git ls-tree bb2fa<br>040000 tree 5bac6559179bd543a024d6d187692343e2d8ae83 copy1<br>040000 tree 5bac6559179bd543a024d6d187692343e2d8ae83 copy2<br>So now we can see that we&#8217;ve created a new tree just from index manipulation. You can also do interesting merge operations and such in a temporary index this way &#8211; see the git read-tree docs for more information.<\/p>\n\n\n\n<p>Creating Commits<br>Now that we have a tree SHA, we can create a commit object that points to it. We can do this using the git commit-tree command. Most of the data that goes into the commit has to be set as environment variables, so you&#8217;ll want to set the following:<\/p>\n\n\n\n<p>GIT_AUTHOR_NAME<br>GIT_AUTHOR_EMAIL<br>GIT_AUTHOR_DATE<br>GIT_COMMITTER_NAME<br>GIT_COMMITTER_EMAIL<br>GIT_COMMITTER_DATE<br>Then you will need to write your commit message to a file or somehow pipe it into the command through STDIN. Then, you can create your commit object based on the tree sha we have.<\/p>\n\n\n\n<p>$ git commit-tree bb2fa &lt; \/tmp\/message<br>a5f85ba5875917319471dfd98dfc636c1dc65650<br>If you want to specify one or more parent commits, simply add the shas on the command line with a &#8216;-p&#8217; option before each. The SHA of the new commit object will be returned via STDOUT.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating BlobsCreating a blob in your Git repository and getting a SHA back is pretty easy. The git hash-object command is all you&#8217;ll need. To create a blob object from&#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-30833","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30833","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=30833"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30833\/revisions"}],"predecessor-version":[{"id":30834,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30833\/revisions\/30834"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=30833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=30833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=30833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}