{"id":1132,"date":"2014-09-18T10:20:14","date_gmt":"2014-09-18T09:20:14","guid":{"rendered":"https:\/\/www.hutsky.cz\/blog\/?p=1132"},"modified":"2014-09-18T10:20:14","modified_gmt":"2014-09-18T09:20:14","slug":"subdirectory-checkout-with-git-sparse-checkout","status":"publish","type":"post","link":"https:\/\/www.hutsky.cz\/blog\/2014\/09\/subdirectory-checkout-with-git-sparse-checkout\/","title":{"rendered":"Subdirectory Checkout with git sparse-checkout"},"content":{"rendered":"<p>It&#8217;s very convenient, even necessary, to use a version control system, e.g. CVS or Git. One can go back in history and easily trace the changes (especially) when something goes wrong. Git, being a distributed system, can be used to develop locally and once your work is ready, you can push it upstream to your repository, where it is available to the others to see or to pull. Next, you&#8217;d probably want to apply the patches to the production server, where the software is actually deployed. Most often, this is done uploading the files via FTP.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1136 size-full\" src=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/git-deployment-1.png\" alt=\"git-deployment-1\" width=\"407\" height=\"298\" srcset=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/git-deployment-1.png 407w, https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/git-deployment-1-300x219.png 300w\" sizes=\"auto, (max-width: 407px) 100vw, 407px\" \/><\/p>\n<p>This approach is common and usually serves well. Over the years, however, I have found that there is space for &#8220;human error&#8221;. It happened a few times that I forgot to upload a file that was part of the patch and had to deal with the consequent problems later. Then it occurred to me that I could make use of the version control features for deployment as well. You need shell access, of course, but where possible, this approach can be very convenient and it can eliminate the human factor.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1140\" src=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/git-deployment-2.png\" alt=\"git-deployment-2\" width=\"407\" height=\"298\" srcset=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/git-deployment-2.png 407w, https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/git-deployment-2-300x219.png 300w\" sizes=\"auto, (max-width: 407px) 100vw, 407px\" \/><\/p>\n<p>Now, it can be useful sometimes not to checkout all the contents of the repository on the production server. Let&#8217;s say you have this directory structure:<\/p>\n<pre>|\r\n|__Documentation\r\n|__SQL_structure_files\r\n|__public_html<\/pre>\n<p>where only the <em>public_html<\/em> directory needs to be deployed. To be able to check out only some part of the repository you can use <em>git sparse-checkout<\/em> in the directory on the production server where you want to put the files:<\/p>\n<pre>git init \r\ngit remote add origin git@repo.example.com:\/git\/repo_name\r\ngit config core.sparsecheckout true\r\necho public_html &gt;&gt; .git\/info\/sparse-checkout\r\ngit pull origin master<\/pre>\n<p>On the first line, you initialize a new repository. Then you add your remote repository to the list of remotes. On the third line, you enable the sparsecheckout feature that allows you to check out partial content only. On the fourth line, you define what directories you want to check out, it&#8217;s only <em>public_html<\/em> in this case. Once you&#8217;re done, you can get the repository contents and checkout the allowed contents, i.e. to pull.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s very convenient, even necessary, to use a version control system, e.g. CVS or Git. One can go back in history and easily trace the changes (especially) when something goes wrong. Git, being a distributed system, can be used to &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.hutsky.cz\/blog\/2014\/09\/subdirectory-checkout-with-git-sparse-checkout\/\"> <span class=\"screen-reader-text\">Subdirectory Checkout with git sparse-checkout<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[40],"class_list":["post-1132","post","type-post","status-publish","format-standard","hentry","category-programming","tag-git"],"_links":{"self":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/comments?post=1132"}],"version-history":[{"count":10,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1132\/revisions"}],"predecessor-version":[{"id":1145,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1132\/revisions\/1145"}],"wp:attachment":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/media?parent=1132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/categories?post=1132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/tags?post=1132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}