{"id":392918,"date":"2022-01-12T13:00:25","date_gmt":"2022-01-12T10:00:25","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-update-and-maintain-separate-git-branches-cloudsavvy-it\/"},"modified":"2022-01-12T13:00:25","modified_gmt":"2022-01-12T10:00:25","slug":"how-to-update-and-maintain-separate-git-branches-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-update-and-maintain-separate-git-branches-cloudsavvy-it\/","title":{"rendered":"#How To Update and Maintain Separate Git Branches \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 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-6a23e93bbb07c\" 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-6a23e93bbb07c\" 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-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-update-and-maintain-separate-git-branches-cloudsavvy-it\/#Why_Maintain_Different_Branches\" >Why Maintain Different Branches?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-update-and-maintain-separate-git-branches-cloudsavvy-it\/#Keeping_Branches_in_Sync_With_Rebasing\" >Keeping Branches in Sync With Rebasing<\/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-to-update-and-maintain-separate-git-branches-cloudsavvy-it\/#Fixing_Unwanted_Commits\" >Fixing Unwanted Commits<\/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-to-update-and-maintain-separate-git-branches-cloudsavvy-it\/#Cherry-Picking_Commits_Onto_Another_Branch\" >Cherry-Picking Commits Onto Another Branch<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How To Update and Maintain Separate Git Branches \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-14290\" data-pagespeed-lazy-srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.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\/2021\/09\/4d72a7db.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>One of Git\u2019s core features is the ability to make multiple versions of your project. Often, these are used for short-term forks called \u201cfeature branches,\u201d which get merged into master. However, sometimes it is necessary to have truly separate branches, which makes it harder to keep them in sync.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Why_Maintain_Different_Branches\"><\/span>Why Maintain Different Branches?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Usually, branches are short lived, and intended to be merged back into the main release branch. In some cases though, it\u2019s necessary to maintain fully separate branches.\u00a0For example, you may have branches targetting different platforms or dependencies, with different features, or just separate release branches that you keep alive for a while.<\/p>\n<p>This is the same workflow with most forks, and depending on the amount and severity of changes, it may be hard to keep in sync with upstream. If your two branches are largely in sync minus a few commits, you\u2019ll have a much easier time handling conflicts and keeping things up to date.<\/p>\n<p>In most cases though, you should look for alternatives to having to separate branches, because it can get quite tedious, especially with merge conflicts and all of the extra testing. For example, if you have two builds targetting different platforms, many languages have build configuration and preprocessors that can handle that.\u00a0C# has <code>#if NETVERSION<\/code>, which allows changes to code based on which platform it\u2019s compiling for.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15204\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/01\/df1d85c9.jpg?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"470\" height=\"244\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Whatever your reason though, this is a valid Git workflow that many people do use.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Keeping_Branches_in_Sync_With_Rebasing\"><\/span>Keeping Branches in Sync With Rebasing<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are basically two options for how to go about this. The first and most common method is rebasing, which is a lot like merging, but allows the branches to be completely independent.<\/p>\n<p>You can think of Git commits like a chain of changes going back in time, each one pointing to the previous commit. When you create a new branch, it breaks off from the main <code>master<\/code> branch at a specific point, the <em>base<\/em> of the branch.<\/p>\n<p>Rebasing is basically lifting up the entire <code>feature<\/code> branch, and moving it to a new point in time, where the end of it points to a different chain of commits. This is most useful if the forked branch only contains a few commits, because then the merge conflicts will be easier to sort out. In this example, rebasing this branch incorporates the B and C commits, but not D and E, as it doesn\u2019t have to rebase onto the HEAD of the branch.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15197\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/01\/0e105445.jpg?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"995\" height=\"407\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Rebasing is commonly used only for local branches though, and presents a few problems when used with shared branches. The commits don\u2019t actually move; they\u2019re copied over, resulting in new commit IDs, with the HEAD of the branch being moved to the new location.<\/p>\n<p>This means that the old commits are left stranded. Because Git is a decentralized version control system, your coworkers could have un-pushed commits that reference those deleted commits. Rebasing will have to be something you coordinate with any collaborators, and if anyone has any conflicts, they will need to fix it locally by copying those changes to the correct location.<\/p>\n<p>Rebasing a branch is pretty easy. You\u2019ll need to checkout the <code>feature<\/code> branch, pull all the changes from your remote, and then run <code>rebase &lt;branch&gt;<\/code>\u00a0to move the <code>feature<\/code> branch onto the target branch.<\/p>\n<pre>git checkout feature&#13;\ngit pull&#13;\ngit rebase master<\/pre>\n<p>This will likely result in merge conflicts, which you will have to resolve yourself and then commit the changes.<\/p>\n<p>While most day-to-day Git tasks can be done pretty easily from the command line, rebasing is by nature a pretty visual operation, so we do recommend using a GUI Git client like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/git-fork.com\/\">Fork<\/a> or <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.gitkraken.com\/\">GitKraken<\/a> if you can. This will show you the branch lines and help you plan the rebase more effectively, and can even perform interactive rebases.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15200\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/01\/b8016375.jpg?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"803\" height=\"275\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Since rebasing basically <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>lies each commit from the feature branch onto a new location, you don\u2019t have to include all of them, and interactive rebasing can drop commits you don\u2019t need. It\u2019s possible to do from the command line, but makes more sense from a GUI.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Fixing_Unwanted_Commits\"><\/span>Fixing Unwanted Commits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>What happens if you don\u2019t want to include some commits when you rebase? If you have commits on your <code>feature<\/code> branch that you\u2019d like to exclude when rebasing onto <code>master<\/code>, then you can do an interactive rebase. This will drop the unwanted commits when rebasing, essentially removing them from history.<\/p>\n<p>But, it\u2019s much more likely that there are commits on the\u00a0<code>master<\/code> branch that you\u2019d rather not have on your <code>feature<\/code> branch. Because rebasing sets the base of the new branch to master, there\u2019s no way to not include those commits.<\/p>\n<p>If there\u2019s just one or two commits that you\u2019d like to not have, you can probably rebase anyway, and either sort it out in the merge conflict, fix it manually, or just revert the commit. Git has a \u201crevert\u201d command that will apply the opposite changes, essentially reversing a commit and making it like it never happened. To use it, run <code>git log<\/code>\u00a0to find the commit you want to revert:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12608\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/07\/5b6c2c8a.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"619\" height=\"277\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Then, copy the SHA1 hash and revert the commit:<\/p>\n<pre>git revert 62ee517cc7c358eafbbffdebdde1b38dea92aa0f<\/pre>\n<p>This will create a \u201crevert commit\u201d on the <code>feature<\/code> branch.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Cherry-Picking_Commits_Onto_Another_Branch\"><\/span>Cherry-Picking Commits Onto Another Branch<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The last scenario you may run into is if there are only a few commits from <code>master<\/code>\u00a0that you\u2019d like to have on <code>feature<\/code>. This is common if you\u2019re maintaining branches for different versions, because there are often hotfixes and patches that need to be applied to older versions of software.<\/p>\n<p>This is the most annoying method of syncing things though, because if you\u2019re not keeping your branches at least somewhat in sync, there\u2019s a high chance that the commit you want to include might be incompatible with the older branch.<\/p>\n<p>But, Git does have tools to essentially copy and paste commits onto a different branch. This action is called cherry-picking, because you\u2019re grabbing a single commit out of the history and plucking it out. Similarly to rebasing, cherry-picking creates new copied commits, but Git is usually smart enough to sort it out, even if you were to merge the branches together later on.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13233\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/3c4e20b0.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1200\" height=\"500\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To cherry pick, you\u2019ll need to grab the commit ID. If your branch history is complicated, you can run <code>git log<\/code>\u00a0with the <code>--graph<\/code>\u00a0option to get a visual representation of your history, though a GUI client is especially useful for tasks like this.<\/p>\n<pre>git log --pretty=format:\"%h %s\" --graph<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13234\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/7f8a78ad.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"883\" height=\"375\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Then, make sure you\u2019re on the <code>feature<\/code> branch, and run <code>cherry-pick<\/code>\u00a0with the commit ID to copy it over.<\/p>\n<pre>git checkout feature&#13;\ngit cherry-pick 1da76d3<\/pre>\n<p>This may result in conflicts, which you will have to resolve.\n<\/p><\/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\/15193\/how-to-update-and-maintain-separate-git-branches\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How To Update and Maintain Separate Git Branches \u2013 CloudSavvy IT&#8221; One of Git\u2019s core features is the ability to make multiple versions of your project. Often, these are used for short-term forks called \u201cfeature branches,\u201d which get merged into master. However, sometimes it is necessary to have truly separate branches, which makes it harder&#8230;<\/p>\n","protected":false},"author":1,"featured_media":392919,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-392918","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\/392918","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=392918"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/392918\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/392919"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=392918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=392918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=392918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}