{"id":3535,"date":"2017-06-15T12:47:52","date_gmt":"2017-06-15T11:47:52","guid":{"rendered":"http:\/\/www.blopig.com\/blog\/?p=3535"},"modified":"2017-06-15T12:50:43","modified_gmt":"2017-06-15T11:50:43","slug":"using-bare-git-repos","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2017\/06\/using-bare-git-repos\/","title":{"rendered":"Using bare git repos"},"content":{"rendered":"<p>Git is a fantastic method of doing version control of your code. Whether it&#8217;s to share with collaborators, or just for your own reference, it almost acts as an absolute point of reference for a wide variety of applications and needs. The basic concept of git is that you have your own folder (in which you edit your code, etc.) and you commit\/push those changes to a git repository. Note that Git is a version control SYSTEM and GitHub\/BitBucket etc. are services that host repositories using Git as its backend!<\/p>\n<p>The basic procedure of git can be summarised to:<\/p>\n<p>1. Change\/add\/delete files in your current working directory as necessary. This is followed by a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">git add<\/code> or <code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">git rm<\/code> command.<br \/>\n2. &#8220;Commit&#8221; those changes; we usually put a message reflecting the change from step 1. e.g. <code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">git commit -m \"I changed this file because it had a bug before.\"<\/code><br \/>\n3. You &#8220;push&#8221; those changes with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">git push<\/code>\u00a0to a git repository (e.g. hosted by BitBucket, GitHub, etc.); this is sort of like saying &#8220;save&#8221; that change.<\/p>\n<p>Typically we use services like GitHub to HOST a repository. We then push our changes to that repository (or <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git pull<\/code> from it) and all is good. However, a\u00a0powerful concept to bear in mind is the &#8216;bare&#8217; git repository. This is especially useful if you have code that&#8217;s private and should be strictly kept within your company\/institution&#8217;s server, yet you don&#8217;t want people messing about too much with the master version of the code. The diagram below makes the bare git repository concept quite clear:<\/p>\n<div id=\"attachment_3536\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2017\/06\/01.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" aria-describedby=\"caption-attachment-3536\" loading=\"lazy\" class=\"size-large wp-image-3536\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2017\/06\/01.png?resize=625%2C427&#038;ssl=1\" alt=\"\" width=\"625\" height=\"427\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2017\/06\/01.png?resize=1024%2C699&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2017\/06\/01.png?resize=300%2C205&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2017\/06\/01.png?resize=768%2C524&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2017\/06\/01.png?resize=624%2C426&amp;ssl=1 624w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2017\/06\/01.png?w=1250&amp;ssl=1 1250w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2017\/06\/01.png?w=1875&amp;ssl=1 1875w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><p id=\"caption-attachment-3536\" class=\"wp-caption-text\">The bare repo acts as a &#8220;master&#8221; version of sorts, and every other &#8220;working&#8221;, or non-bare repo pushes\/pulls changes out of it.<\/p><\/div>\n<p>Let&#8217;s start with the easy stuff first. Every git repository (e.g. the one you&#8217;re working on in your machine) is a <strong>WORKING\/NON-BARE<\/strong> git repository. This shows files in your code as you expect it, e.g. *.py or *.c files, etc. A\u00a0<strong>BARE\u00a0<\/strong>repository is a folder hosted by a server which only holds git OBJECTS. In it, you&#8217;ll never see a single .py or .c file, but a bunch of folders and text files that look nothing like your code. By the magic of git, this is easily translated as .py or .c files (basically a version of the working repo) when you <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git clone<\/code> it. Since the bare repo doesn&#8217;t contain any of the actual code, you can safely assume that no one can really mess up with the master version without having gone through the process of <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git add\/commit\/push<\/code>, making everything documented. To start a bare repo&#8230;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\"># Start up a bare repository in a server\r\nuser@server:$~  git init --bare name_to_repo.git\r\n\r\n# Go back to your machine then clone it\r\nuser@machine:$~ git clone user@server:\/path\/to\/repo\/name_to_repo.git .\r\n\r\n# This will clone a empty git repo in your machine\r\ncd name_to_repo\r\nls\r\n# Nothing should come up.\r\n\r\ntouch README\r\necho \"Hello world\" &gt;&gt; README\r\ngit add README\r\ngit commit -m \"Adding a README to initialise the bare repo.\"\r\ngit push origin master # This pushes to your origin, which is user@server:\/path\/to\/repo\/name_to_repo.git<\/pre>\n<p>If we check our folders, we will see the following:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">user@machine:$~ ls \/path\/to\/repo\r\nREADME # only the readme exists\r\n\r\nuser@server:$~ ls \/path\/to\/repo\/name_to_repo.git\/\r\nbranches\/ config description HEAD hooks\/ info\/ objects\/ refs\/<\/pre>\n<p>Magic! README isn&#8217;t in our git repo. Again, this is because the git repo is BARE and so the file we pushed won&#8217;t exist. But when we clone it in a different machine&#8230;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">user@machine2:$~ git clone user@server:\/path\/to\/repo\/name_to_repo.git .\r\nls name_to_repo.git\/\r\nREADME\r\ncat README\r\nHello world #magic!<\/pre>\n<p>This was a bit of a lightning tour but hopefully you can see that the purpose of a bare repo is to allow you to host code as a &#8220;master version&#8221; without having you worry that people will see the contents directly til they do a git clone. Once they clone, and push changes, everything will be documented via git, so you&#8217;ll know exactly what&#8217;s going on!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git is a fantastic method of doing version control of your code. Whether it&#8217;s to share with collaborators, or just for your own reference, it almost acts as an absolute point of reference for a wide variety of applications and needs. The basic concept of git is that you have your own folder (in which [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29],"tags":[],"ppma_author":[511],"class_list":["post-3535","post","type-post","status-publish","format-standard","hentry","category-code"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":511,"user_id":22,"is_guest":0,"slug":"jinwoo","display_name":"Jinwoo Leem","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/65d338dc0b03d3026aa9a98f5e43889ca6c9ac9d0f45fe65ea5931207597ce2d?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/3535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=3535"}],"version-history":[{"count":4,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/3535\/revisions"}],"predecessor-version":[{"id":3540,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/3535\/revisions\/3540"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=3535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=3535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=3535"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=3535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}