{"id":507,"date":"2019-11-14T06:06:35","date_gmt":"2019-11-14T14:06:35","guid":{"rendered":"http:\/\/blog.nillsf.com\/?p=507"},"modified":"2019-11-10T12:54:53","modified_gmt":"2019-11-10T20:54:53","slug":"git-branching-explained","status":"publish","type":"post","link":"https:\/\/blog.nillsf.com\/index.php\/2019\/11\/14\/git-branching-explained\/","title":{"rendered":"Git branching explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I am not a developer. I&#8217;m more of an infrastructure person, who delves into development sometimes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As part of a hackathon last week, my team and I ended up needing to do branches in git, and I had to shamefully admit that I always check-in to master. Most developers will be shooting me the same look through their screen as my team shot me when I said that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Well, that will never happen again! Let me show you what I learned about branching in Git, and how to do it. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why branching in git<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Branches in git allow you to protect your code. When you start working on a new feature and you create a new branch, you can work in isolation from your team. This means, if you make changes in your branch that break something, other people are not impacted by this. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The moment your work is done, you need to open a pull request to the master branch. Opening a pull request, will allow your branch to be merged with the master branch. Once your pull request actually gets merged, everybody who creates a new branch will use the work you did and merged into master.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I don&#8217;t want to in depth on different branching (and merging) strategies, but if you&#8217;re looking for more, these two pieces of information helped me learn about branching:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/devops\/learn\/devops-at-microsoft\/use-git-microsoft\">How Microsoft uses Git<\/a><\/li><li><a href=\"https:\/\/www.youtube.com\/watch?v=t_4lLR6F_yk\">Git patterns and anti-patterns for successful developers<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How to create, work on and merge a branch<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For this demo, I&#8217;ll create a branch of my <a href=\"https:\/\/github.com\/nillsf\/blog\">blog repo<\/a> in Github. I use this repo to share code examples I build for demos.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, let&#8217;s create a local branch first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git checkout -b \"branching_blog_post\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above command will do two things. First it will create a new branch locally called branching_blog_post, and secondly will switch my local git to make all changes to that directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we&#8217;ll do some work in that branch. I&#8217;ll create a new directory, and add a README.md file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we can use the <code>git status<\/code> command to show us what the difference is between what is on my file system and what is checked in to git.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git status\nOn branch branching_blog_post\nUntracked files:\n  (use \"git add &lt;file>...\" to include in what will be committed)\n\n        branching\/\n\nnothing added to commit but untracked files present (use \"git add\" to track)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we can use the <code>git add<\/code> command to add those folders and files to our repo.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git add branching\ngit status\nOn branch branching_blog_post\nChanges to be committed:\n  (use \"git reset HEAD &lt;file>...\" to unstage)\n\n        new file:   branching\/README.md<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we have changes to be committed, we can commit those to our local branch.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git commit -m \"added README.md\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, an important remark here. All changes are still local to my local git repo. They haven&#8217;t been pushed up to github.com yet. Commiting locally can help you keep track of the work you&#8217;ve actually done and give you the ability to rollback. To make our changes appear in github.com, we&#8217;ll need to use git push. However, out github repo doesn&#8217;t have this branch yet. When executing a simple git push, git will give you a command explaining how to make and push those changes to github.com<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git push --set-upstream origin branching_blog_post<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we have our branch in github.com. We can switch to that branch to actually see that branch in github.com &#8211; and we can create a pull request on github.com to compare code and try to merge into master.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"994\" height=\"585\" src=\"\/wp-content\/uploads\/2019\/11\/image-17.png\" alt=\"\" class=\"wp-image-508\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-17.png 994w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-17-300x177.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-17-768x452.png 768w\" sizes=\"auto, (max-width: 994px) 100vw, 994px\" \/><figcaption>Our branch on github.com.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll now use the github.com website to make a pull request and merge into master. We&#8217;ll git the Compare &amp; pull request button in the visual interface to do this. The interface is self-explanatory. You give the right level of information, and you create the pull request.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, in our example this is just text. In real projects you typically see a PR build happening now to make sure that your PR succeeds a build definition and can pass certain tests. You can also assign mandatory code reviews on a PR, to ensure your code is up to standards. But in our demo, nothing of that is implemented.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"999\" height=\"604\" src=\"\/wp-content\/uploads\/2019\/11\/image-18.png\" alt=\"\" class=\"wp-image-509\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-18.png 999w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-18-300x181.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-18-768x464.png 768w\" sizes=\"auto, (max-width: 999px) 100vw, 999px\" \/><figcaption>The visual interface will help in giving the right information for the pull request.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And finally, we can do the actual merge. Again, in real life people can make comments and ask you to make changes to your PR, but in our demo we&#8217;ll simply merge.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"749\" height=\"667\" src=\"\/wp-content\/uploads\/2019\/11\/image-19.png\" alt=\"\" class=\"wp-image-510\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-19.png 749w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-19-300x267.png 300w\" sizes=\"auto, (max-width: 749px) 100vw, 749px\" \/><figcaption>And now we are ready to merge.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now, your branch is merged into master. Typically, you would now go ahead and delete the branch. For new work, you&#8217;ll create a new branch anyway.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"751\" height=\"100\" src=\"\/wp-content\/uploads\/2019\/11\/image-20.png\" alt=\"\" class=\"wp-image-511\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-20.png 751w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-20-300x40.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-20-750x100.png 750w\" sizes=\"auto, (max-width: 751px) 100vw, 751px\" \/><figcaption>You can now go ahead and delete the branch.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Branching in git enables you to make changes to code without impacting your colleagues and keeps you code clean. In this post, we walked through a simple example of doing git branching. We used the command line to create and push the branch, and used github.com to merge the branch into master.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am not a developer. I&#8217;m more of an infrastructure person, who delves into development sometimes. As part of a hackathon last week, my team and I ended up needing to do branches in git, and I had to shamefully admit that I always check-in to master. Most developers will be shooting me the same [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":510,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5,31],"tags":[54,52,53,55],"class_list":["post-507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-open-source","category-software-development","tag-branching","tag-devops","tag-github","tag-software-development"],"jetpack_featured_media_url":"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-19.png","_links":{"self":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/comments?post=507"}],"version-history":[{"count":1,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":512,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/507\/revisions\/512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/media\/510"}],"wp:attachment":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/media?parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/categories?post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/tags?post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}