{"id":415899,"date":"2022-03-14T14:30:55","date_gmt":"2022-03-14T11:30:55","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-do-git-branches-work-cloudsavvy-it\/"},"modified":"2022-03-14T14:30:55","modified_gmt":"2022-03-14T11:30:55","slug":"how-do-git-branches-work-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-do-git-branches-work-cloudsavvy-it\/","title":{"rendered":"#How Do Git Branches Work? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3219e789590\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3219e789590\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-do-git-branches-work-cloudsavvy-it\/#%E2%80%9CHow_Do_Git_Branches_Work_%E2%80%93_CloudSavvy_IT%E2%80%9D\" >&#8220;How Do Git Branches Work? \u2013 CloudSavvy IT&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-do-git-branches-work-cloudsavvy-it\/#What_Are_Branches_Really\" >What Are Branches, Really?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-do-git-branches-work-cloudsavvy-it\/#Merging_and_Rebasing\" >Merging and Rebasing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-do-git-branches-work-cloudsavvy-it\/#How_Do_You_Use_Branches\" >How Do You Use Branches?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_Do_Git_Branches_Work_%E2%80%93_CloudSavvy_IT%E2%80%9D\"><\/span>&#8220;How Do Git Branches Work? \u2013 CloudSavvy IT&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-15727\" data-pagespeed-lazy-srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/5ea21a83.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/5ea21a83.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/5ea21a83.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1200\" height=\"675\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Branches are a core feature of Git\u2019s version tracking and are used constantly by teams working on the same software codebase. We\u2019ll dive into how they work under the hood, and how you can use them to improve your Git workflow.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Are_Branches_Really\"><\/span>What Are Branches, Really?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Branches are used to split off Git history. You can think of Git commits like a line of changes going back in time. You can \u201ccheckout\u201d any of these commits and move your local directory back in time to the state it was at when that commit was made.<\/p>\n<p>Branches are commonly used for working on experimental features, or changes that take a while, or anything else that might otherwise break the repository. For example, you may be working on refactoring a big component of your codebase, and until it\u2019s done, you want <code>master<\/code>\u00a0branch to be stable.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15728\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/9e070fec.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1117\" height=\"369\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Once the new <code>feature<\/code>\u00a0branch is stable, it can be merged back into <code>master<\/code>, often through a\u00a0<em>pull request<\/em>, which is a process that allows for code review and testing to be done before changes are made.<\/p>\n<p>However, under the hood, branches work a bit differently than you might first expect. In Git, branches are just labels, or pointers, to a specific commit. That\u2019s it, the <code>master<\/code>\u00a0branch simply points to the latest commit made on <code>master<\/code>; when you make a new commit, the label is updated to point to the new commit.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15729\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/c6fd663e.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"886\" height=\"465\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>While it\u2019s useful to think of commits as moving forward in time; in reality, Git commits point backwards to each other. Each commit has a reference to the last commit, and this chain is used to construct the state of the repository.<\/p>\n<p>If you make a new branch though, things work a little differently. Whichever branch you have checked out (with <code>git checkout &lt;branch&gt;<\/code>) will be used as the label for the new commit.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15730\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/6f21faf7.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"876\" height=\"348\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To make the branch in this example, you must first make sure your repository HEAD is set to the <code>master<\/code>\u00a0branch. This is because you can actually make branches starting from anywhere\u2014including past commits or commits on other branches.<\/p>\n<pre>git checkout master<\/pre>\n<p>Then make a new branch, and swap to it:<\/p>\n<pre>git branch feature&#13;\ngit checkout feature<\/pre>\n<p>At this point, nothing in your repository has changed. Both the <code>feature<\/code>\u00a0and <code>master<\/code>\u00a0branch labels point to the same commit.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15731\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/19c3d2d2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"790\" height=\"330\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>However, anything you commit from this point on will be added to the <code>feature<\/code>\u00a0branch. More specifically, a new commit will be created, set to point back towards the current commit, and the \u201cfeature\u201d label will be updated to point to this new commit.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15733\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/d0b7b6b7.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1137\" height=\"363\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You could even <code>checkout master<\/code>\u00a0and make more commits on the main branch. It won\u2019t affect the <code>feature<\/code>\u00a0branch, because all the label knows is that it points to that specific commit. It won\u2019t be updated with the <code>master<\/code>\u00a0label.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15732\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/da18e24a.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"912\" height=\"341\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Merging_and_Rebasing\"><\/span>Merging and Rebasing<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Of course, branches wouldn\u2019t be too useful if they were stuck there forever, so Git provides tools to merge them back into the <code>master<\/code>\u00a0branch. Technically you can merge sub-branches into any other branch, as long as the histories are compatible.<\/p>\n<p>The simplest case is where you have a simple branch that just needs to be merged back. You can checkout the <code>master<\/code>\u00a0branch, then run <code>git merge feature<\/code>\u00a0to \u201creplay\u201d all the commits made on the feature branch onto master.<\/p>\n<p>This incorporates them into the main timeline and creates a new \u201cmerge commit\u201d with the changes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15735\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/3fa7531c.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1013\" height=\"436\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>It\u2019s not always that simple though, and in many cases, you will have\u00a0<em>merge conflicts<\/em> that need to be resolved. This can include branches modifying the same lines in files, file moves or deletion, or other kinds of bugs that come up when the software is changed since the <code>feature<\/code>\u00a0branch was created.<\/p>\n<p>If you have a long-running <code>feature<\/code>\u00a0branch, one way to minimize this problem is to perform frequent merges, in reverse this time\u2014from <code>master<\/code>\u00a0into <code>feature<\/code>. This keeps <code>feature<\/code>\u00a0up to date, and while it\u00a0doesn\u2019t really reduce the workload required, it prevents it from piling up into a huge mess.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15736\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/10d45734.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1063\" height=\"417\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This strategy is common for long-lived branches and is <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly considered best practice for Git.<\/p>\n<p>Another tool that is also used in this scenario is\u00a0<em>rebasing.<\/em> Basically, rebasing is like picking up the entire branch and moving it to start from a new location, often the latest commit in the repository. This leads to a cleaner Git history in some cases and is the preferred solution for some complicated situations.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15737\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/9bf517bb.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"998\" height=\"412\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>However, Git history is \u201cimmutable,\u201d and because of this rebasing copies commits instead of actually moving them. This can lead to lots of problems on shared branches, if not coordinated properly with your team\u2014if you rebase, and your coworker makes a new commit on the \u201cold,\u201d now deleted feature branch, it will be left stranding. They will have to stash the commit and pop it on the new branch to reconcile the changes.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_Do_You_Use_Branches\"><\/span>How Do You Use Branches?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To start making a new branch, you\u2019ll want to put your repository in the proper state so that the new branch label starts where you want it to. If you\u2019re branching off of <code>master<\/code>, just checkout the entire branch to start at the latest commit. Otherwise, you can put your repo in a detached HEAD state by checking out an individual commit.<\/p>\n<pre>git checkout master&#13;\n&#13;\ngit checkout\u00a0aa3e570923b8ee61414cec17d9033faab4f084a6<\/pre>\n<p>Then, you can make the new branch, and switch to it with checkout:<\/p>\n<pre>git branch feature&#13;\ngit checkout feature<\/pre>\n<p>This can be done in one command, with the <code>-b<\/code>\u00a0flag to checkout:<\/p>\n<pre>git checkout -b feature<\/pre>\n<p>At this point, any commits made in your repo will be made to the new branch.<\/p>\n<p>If you need to swap branches again, just run <code>git checkout master<\/code>\u00a0to be set back to normal.<\/p>\n<p>If you have local changes that you need to move, you can put them in <code>git stash<\/code>. The changes will be stored and can be re<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lied after swapping branches.<\/p>\n<pre>git stash &#13;\ngit checkout feature&#13;\ngit stash apply<\/pre>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14038\/how-do-git-branches-work\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How Do Git Branches Work? \u2013 CloudSavvy IT&#8221; Branches are a core feature of Git\u2019s version tracking and are used constantly by teams working on the same software codebase. We\u2019ll dive into how they work under the hood, and how you can use them to improve your Git workflow. What Are Branches, Really? Branches are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":415900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/5ea21a83.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-415899","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/415899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=415899"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/415899\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/415900"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=415899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=415899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=415899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}