{"id":311,"date":"2020-01-09T14:20:57","date_gmt":"2020-01-09T08:50:57","guid":{"rendered":"https:\/\/openapex.org\/spaces\/software\/?p=311"},"modified":"2023-08-11T12:56:24","modified_gmt":"2023-08-11T07:26:24","slug":"git-refresh-feature-branch-from-master","status":"publish","type":"post","link":"https:\/\/mutesoft.com\/spaces\/software\/git-refresh-feature-branch-from-master.html","title":{"rendered":"Git &#8211; Refresh Feature Branch from Master &#8211; Rebase Vs. Merge"},"content":{"rendered":"\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"628\" src=\"https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/02\/git-e1672746066670.png\" alt=\"\" class=\"wp-image-1053\" srcset=\"https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/02\/git-e1672746066670.png 1200w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/02\/git-e1672746066670-300x157.png 300w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/02\/git-e1672746066670-1024x536.png 1024w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/02\/git-e1672746066670-768x402.png 768w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/02\/git-e1672746066670-500x262.png 500w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/02\/git-e1672746066670-800x419.png 800w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<\/div><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:\/\/mutesoft.com\/spaces\/software\/git-refresh-feature-branch-from-master.html\/#1_Context\" >1. Context<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/mutesoft.com\/spaces\/software\/git-refresh-feature-branch-from-master.html\/#2_Working_with_Feature_Branch\" >2. Working with Feature Branch<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/mutesoft.com\/spaces\/software\/git-refresh-feature-branch-from-master.html\/#3_Rebase\" >3. Rebase<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/mutesoft.com\/spaces\/software\/git-refresh-feature-branch-from-master.html\/#4_Merge\" >4. Merge<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/mutesoft.com\/spaces\/software\/git-refresh-feature-branch-from-master.html\/#5_Recommendation\" >5. Recommendation<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"1-context\"><span class=\"ez-toc-section\" id=\"1_Context\"><\/span><strong>1. Context<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It&#8217;s a good practice to work with branches instead of directly working with the master or main. For every feature and bug you work on, create a distinct branch to isolate your work. Feature branches are supposed to be short-lived. Once your code is merged with the master, delete it. However, while working on a feature branch, often you need to get the latest changes from master (contributed by other developers) to your feature branch to test your code with changes made by other developers.<\/p>\n\n\n\n<p>There are two ways to refresh your feature branch &#8211; <code>merge<\/code> and <code>rebase<\/code>. In this article, we&#8217;ll explore both options.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-working-with-feature-branch\"><span class=\"ez-toc-section\" id=\"2_Working_with_Feature_Branch\"><\/span>2. <strong>Working with Feature Branch<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Create a feature branch, make some changes, and push to the branch.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Create a feature branch\ngit checkout -b &lt;feature-branch>\n\n# Make some changes and stage\ngit add &lt;files>\n\n# Commit your changes\ngit commit -m \"Some message\"\n\n# Submit your changes to the feature branch\ngit push origin &lt;feature-branch><\/pre>\n\n\n\n<p>Now, the master and the feature branch diverged.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/openapex.org\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"270\" src=\"https:\/\/openapex.org\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-1.png\" alt=\"\" class=\"wp-image-344\" srcset=\"https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-1.png 522w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-1-300x155.png 300w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/a><figcaption class=\"wp-element-caption\">Master and Feature branches diverged. Courtesy: www.atlassian.com<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-rebase\"><span class=\"ez-toc-section\" id=\"3_Rebase\"><\/span>3. <strong>Rebase<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/openapex.org\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"326\" src=\"https:\/\/openapex.org\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-3.png\" alt=\"\" class=\"wp-image-346\" srcset=\"https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-3.png 522w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-3-300x187.png 300w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/a><figcaption class=\"wp-element-caption\">After Rebase operation. Courtesy: www.atlassian.com<\/figcaption><\/figure>\n\n\n\n<p>Rebase moves all diverging commits of the feature branch to the top by creating new hashes for the commits. The commit history is re-written, which makes it linear and clean. Because of this, subsequent code push to the feature branch has to be forced using <code>--force<\/code> option. <\/p>\n\n\n\n<p>In interactive mode (<code>rebase -i<\/code>) however, we can re-organize the commit history as desired.<\/p>\n\n\n\n<p>Steps for rebasing:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Switch to master and get latest files\ngit checkout master\ngit pull\n\n# Switch to your branch and rebase to get changes\ngit checkout &lt;feature-branch>\ngit rebase master\n\n# Resolve merge conflicts and push to your branch\n# Use force flag if you have already pushed changes to the branch before\ngit push origin &lt;feature-branch> --force<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-merge\"><span class=\"ez-toc-section\" id=\"4_Merge\"><\/span>4. <strong>Merge<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><a href=\"https:\/\/openapex.org\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"326\" src=\"https:\/\/openapex.org\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-4.png\" alt=\"\" class=\"wp-image-347\" srcset=\"https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-4.png 522w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-4-300x187.png 300w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/a><figcaption class=\"wp-element-caption\">After Merge operation. Courtesy: www.atlassian.com<\/figcaption><\/figure>\n\n\n\n<p>Merge applies all commits on top of the feature branch and creates a new merge commit in the feature branch, keeping the feature branch&#8217;s commit history intact. It means the feature branch will have an extra merge commit every time we perform the merge operation.<\/p>\n\n\n\n<p>Steps for merging:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Switch to master and get latest files\ngit checkout master\ngit pull\n\n# Switch to your branch and merge to get changes\ngit checkout &lt;feature-branch>\ngit merge master\n\n# Resolve merge conflicts and push to your branch\ngit push origin &lt;feature-branch><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-recommendation\"><span class=\"ez-toc-section\" id=\"5_Recommendation\"><\/span>5. <strong>Recommendation<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Since <code>rebase<\/code> re-writes the history, it is hard to understand when changes from the master were incorporated into the feature branch. For public repositories, <code>rebase<\/code> is not recommended at all. On the other hand, <code>merge<\/code> creates an extra commit. The merge commit helps to track when the changes were incorporated and it keeps the history intact. <strong>Therefore, in order to refresh your feature branch from the master <\/strong><code>merge<\/code> <strong>is recommended<\/strong>.<\/p>\n\n\n\n<p>Note that the same recommendation applies to refresh any branch from any other branch, not only while refreshing the feature branch from the master.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working on a feature branch, often you need to get the latest changes from master (contributed by other developers) to your feature branch to test your code with changes made by other developers.<\/p>\n","protected":false},"author":2,"featured_media":344,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"","_seopress_titles_desc":"While working on a feature branch, often you need to get the latest changes from master (contributed by other developers) to your feature branch to test your code with changes made by other developers.","_seopress_robots_index":"","_vp_format_video_url":"","_vp_image_focal_point":[],"footnotes":""},"categories":[32],"tags":[31],"class_list":["post-311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tools","tag-git"],"_links":{"self":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts\/311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/comments?post=311"}],"version-history":[{"count":28,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":1519,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts\/311\/revisions\/1519"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/media\/344"}],"wp:attachment":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}