{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/git-rebase-vs-git-merge/","result":{"data":{"markdownRemark":{"id":"3a33f6f5-df0c-55f0-8d4e-c898e2a8cd12","excerpt":"Many of us have to choose between Merge and Rebase as a Developer. Here I'll explain what merge and rebase are, why you should use them, and how to do so. The…","html":"<p>Many of us have to choose between Merge and Rebase as a Developer. Here I'll explain what merge and rebase are, why you should use them, and how to do so.</p>\n<p>The same function is fulfilled by Git Merge and Git Rebase. They are built to incorporate adjustments into one from different divisions. Although the end objective is the same, it is done in various ways by those two approaches, and it is useful to know the difference as you become a better developer of software.</p>\n<h2 id=\"what-are-git-merge-and-git-rebase\" style=\"position:relative;\"><a href=\"#what-are-git-merge-and-git-rebase\" aria-label=\"what are git merge and git rebase permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What are Git Merge and Git Rebase?</h2>\n<p><em>Git Merge and Git Rebase are both used to combine the changes of branches but in a distinct way.</em></p>\n<p><strong>Git Merge</strong> - For developers using version control systems, merging is a prevalent method. Merging takes the contents of a source branch and combines them with a target branch, to be more precise. Only the <a href=\"https://medium.com/@loginradius/how-to-fetch-a-remote-branch-using-git-5c0f6d28264d\">target branch is updated</a> in this process. The history of the source branch remains similar.</p>\n<p><strong>Git Rebase</strong> - Another way to integrate modifications from one branch to another is by Rebase. Rebase compresses all the modifications into a single patch. The patch is then inserted into the target branch.</p>\n<h3 id=\"git-merge\" style=\"position:relative;\"><a href=\"#git-merge\" aria-label=\"git merge permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Git Merge</h3>\n<p>Combine multiple branch histories together.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">  A---B---C feature</span>\n<span class=\"grvsc-line\"> /</span>\n<span class=\"grvsc-line\">D---E---F---G master </span></code></pre>\n<p>if we are merging feature and master branch, then it would be</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">  A---B---C feature</span>\n<span class=\"grvsc-line\"> /               \\</span>\n<span class=\"grvsc-line\">D---E---F---G---H master</span></code></pre>\n<p><strong>Commands</strong> for merging feature and master branch</p>\n<p><code>git checkout feature</code>\n<code>git merge master</code></p>\n<p><strong>Note:</strong></p>\n<ul>\n<li>merge executes only one new commit</li>\n<li>if the feature branch has a completely different development </li>\n<li>If you get conflict while merging\n<code>resolve conflicts</code>\n<code>git add (changes)</code>\n<code>git merge --continue</code></li>\n</ul>\n<h3 id=\"git-rebase\" style=\"position:relative;\"><a href=\"#git-rebase\" aria-label=\"git rebase permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Git Rebase</h3>\n<p>Reapply commits on top of the base branch</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">   A---B---C feature</span>\n<span class=\"grvsc-line\"> /</span>\n<span class=\"grvsc-line\">D---E---F---G master</span></code></pre>\n<p>if we are rebasing the feature branch onto the master branch, then it would be</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">\t   A&#39;--B&#39;--C&#39; feature</span>\n<span class=\"grvsc-line\">\t /</span>\n<span class=\"grvsc-line\">\tD---E---F---G master</span></code></pre>\n<p><strong>Commands</strong> for merging feature and master branch</p>\n<p><code>git checkout feature</code>\n<code>git rebase master</code></p>\n<p><strong>Note:</strong> </p>\n<ul>\n<li>rebase typically executes multiple (number of commits in current branch).</li>\n<li>If you get conflict while rebasing\n<code>resolve conflicts</code>\n<code>git add (changes)</code>\n<code>git rebase --continue</code></li>\n</ul>\n<h2 id=\"when-to-use-git-rebase-or-git-merge\" style=\"position:relative;\"><a href=\"#when-to-use-git-rebase-or-git-merge\" aria-label=\"when to use git rebase or git merge permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>When to use Git Rebase or Git Merge</h2>\n<h3 id=\"choose-merge\" style=\"position:relative;\"><a href=\"#choose-merge\" aria-label=\"choose merge permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Choose Merge</h3>\n<ul>\n<li>whenever we want to add changes of a feature branch back into the base branch.</li>\n<li>if you want to keep the same history rather than rewrite it.</li>\n<li>if you want to revert the changes quickly</li>\n</ul>\n<h3 id=\"choose-rebase\" style=\"position:relative;\"><a href=\"#choose-rebase\" aria-label=\"choose rebase permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Choose Rebase</h3>\n<ul>\n<li>whenever we want to add changes of a base branch back to a feature branch.</li>\n<li>squash multiple commits</li>\n<li>reiterate each commit and update the changes</li>\n<li>reverting rebase would be very difficult</li>\n</ul>\n<h3 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h3>\n<p>I hope some perspectives on Git merge and Git rebase have been provided by this description. The strategy of merge vs rebase is still debatable. But maybe this article will help to dispel your doubts and encourage you to take an approach that works for your team. Here are few articles posted by me that will help you in learning few more things in Git:</p>\n<ol>\n<li><a href=\"https://www.loginradius.com/blog/engineering/git-cherry-pick/\">How to use Git Cherry Pick</a></li>\n<li><a href=\"https://www.loginradius.com/blog/engineering/git-pull-force/\">How to Perform a Git Force Pull</a></li>\n</ol>\n<p>Do comment on the subjects that you would like me to write about in the coming weeks :)</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","headings":[{"value":"What are Git Merge and Git Rebase?","depth":2},{"value":"Git Merge","depth":3},{"value":"Git Rebase","depth":3},{"value":"When to use Git Rebase or Git Merge","depth":2},{"value":"Choose Merge","depth":3},{"value":"Choose Rebase","depth":3},{"value":"Conclusion","depth":3}],"fields":{"slug":"/engineering/git-rebase-vs-git-merge/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"In this post, I will give you a walkthrough on merge and rebase, comparing Git rebase vs. Git merge to find out the similarities and differences.","title":"Git merge vs. Git Rebase: What's the difference?","canonical":null,"date":"January 27, 2021","updated_date":null,"tags":["GIT"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/23c891d4a8760d428bfc6f05aa86f387/2ad7f/merge.webp","srcSet":"/static/23c891d4a8760d428bfc6f05aa86f387/1c9b5/merge.webp 200w,\n/static/23c891d4a8760d428bfc6f05aa86f387/f1752/merge.webp 400w,\n/static/23c891d4a8760d428bfc6f05aa86f387/2ad7f/merge.webp 800w,\n/static/23c891d4a8760d428bfc6f05aa86f387/e7405/merge.webp 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Abhimanyu Singh Rathore","github":"abhir9","bio":"He is a fun-loving technocrat, artist, photographer, nature lover, leisure traveler, and developer. He actively develops full-stack apps and programs in Go and various JavaScript frameworks and libraries, especially React.","avatar":null}}}},"pageContext":{"id":"3a33f6f5-df0c-55f0-8d4e-c898e2a8cd12","fields__slug":"/engineering/git-rebase-vs-git-merge/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}