{"id":718,"date":"2020-11-18T09:44:15","date_gmt":"2020-11-18T08:44:15","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=718"},"modified":"2022-08-23T15:22:59","modified_gmt":"2022-08-23T13:22:59","slug":"git-rebase-vs-merge","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2020\/11\/18\/git-rebase-vs-merge\/","title":{"rendered":"Git Rebase vs Merge"},"content":{"rendered":"\n<p>When you are using git, most probably you may used git merge or git rebase to combine two branches. Pretty much both of these commands do the same thing, but in a different way. This post is to compare the difference between git merge vs rebase and when to use.<\/p>\n\n\n\n<p><br>Think about what happens if you&#8217;re working on a new feature in a dedicated branch and another team member updates the master branch with new commits. This leads to a split-off history.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/wac-cdn.atlassian.com\/dam\/jcr:01b0b04e-64f3-4659-af21-c4d86bc7cb0b\/01.svg?cdnVersion=1339\" alt=\"A forked commit history\" width=\"442\" height=\"253\"\/><\/figure>\n\n\n\n<p><br>Let&#8217;s say that the new commits in master are relevant to the feature you&#8217;re working on. To integrate the new commits into your feature branch, you have two options: merging or rebasing. <\/p>\n\n\n\n<p>the second variante also, Once you completed the feature implementation on feature branch, you need to combine it with the latest master branch. Either you can merge or rebase feature branch with the master.<\/p>\n\n\n\n<h4>Die Merging-Option<\/h4>\n\n\n\n<p>Let\u00b4\u00b4 s say we are merging branche &#8220;master&#8221; into the feature branch , This creates a new \u201cmerge commit\u201d in the feature branch that ties together the histories of both branches, giving you a branch structure that looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/wac-cdn.atlassian.com\/dam\/jcr:e229fef6-2c2f-4a4f-b270-e1e1baa94055\/02.svg?cdnVersion=1339\" alt=\"Merging master into the feature branch\" width=\"361\" height=\"241\"\/><\/figure>\n\n\n\n<p>Merging is nice because it\u2019s a\u00a0<strong><em>non-destructive<\/em><\/strong>\u00a0operation. The existing branches are not changed in any way.\u00a0<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/08\/image-7.png\" alt=\"\" class=\"wp-image-2113\" width=\"438\" height=\"602\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/08\/image-7.png 524w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/08\/image-7-218x300.png 218w\" sizes=\"(max-width: 438px) 100vw, 438px\" \/><\/figure><\/div>\n\n\n\n<p><strong>After a merge, we have a single new commit on the branch we merge into. This commit contains all the changes from the source branch.<\/strong>  <\/p>\n\n\n\n<p>You can merge master branch into the feature branch by running following command:<\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\">#Merge aus Master nach Feature<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git checkout feature\ngit merge master<\/code><\/pre>\n\n\n\n<p>or with single line<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git merge feature master<\/code><\/pre>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\">#Merge aus Master nach Feature, bei dem im Konflitktfall immer Feature gewinnt<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git checkout feature\ngit merge -s ours master<\/code><\/pre>\n\n\n\n<h4>Git Rebase<\/h4>\n\n\n\n<p>Rebase is little different than merge. Rebase apply all feature branch changes on top of master branch by creating new commit for each of its previous commit messages. Which means that rebase command will change your commit history and regenerate commits again on top of master branch. Final output of git rebase can be represent as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/wac-cdn.atlassian.com\/dam\/jcr:5b153a22-38be-40d0-aec8-5f2fffc771e5\/03.svg?cdnVersion=1339\" alt=\"Rebasing the feature branch onto master\" width=\"310\" height=\"212\"\/><\/figure>\n\n\n\n<p>Following command can be used to rebase feature branch on top of master branch. &#8220;-i&#8221; option used for interactive rebase. Otherwise you can simply use &#8220;git rebase master&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git checkout feature\ngit rebase -i master<\/code><\/pre>\n\n\n\n<p><em><span class=\"has-inline-color has-vivid-red-color\">The major benefit of rebasing is that you get a much cleaner project history:<\/span><\/em><\/p>\n\n\n\n<ol><li>First, it eliminates the unnecessary merge commits required by&nbsp;<code>git merge<\/code><\/li><li>Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history \u2014 you can follow the tip of feature all the way to the beginning of the project without any forks<\/li><\/ol>\n\n\n\n<p>This makes it easier to navigate your project with commands like&nbsp;<code>git log<\/code>,&nbsp;<code>git bisect<\/code>, and&nbsp;<code>gitk<\/code>.<\/p>\n\n\n\n<h4>Rebase or Merge<\/h4>\n\n\n\n<p>You can use either rebase or merge to combine two branches. Peoples are tend to use rebase over merge due to following facts.<\/p>\n\n\n\n<ul><li>There is no additional commit added to the merged branches<\/li><li>Rebase commit history is cleaner than merge history since rebase history does not have complex branches<\/li><li>Due to the branch complexity in merged git tree, some code changes are invisible. But rebase changes come from a specific and entitled commit.<\/li><li>Rebase make easier to use git log, git bisect, and gitk commands.<\/li><\/ul>\n\n\n\n<h4>When to not to use Rebase<\/h4>\n\n\n\n<p>As we already discussed, rebase change the commit history. Therefore, it should not be apply on the public branches(eg: master) where other people are working on. This make git confuse that your master branch diverge from others master branch. So, before you run&nbsp;<code>git rebase<\/code>, always ask yourself, \u201cIs anyone else looking at this branch?\u201d. If the answer is yes, take your hands off the keyboard and start thinking about a non-destructive way to make your changes. Otherwise, you\u2019re safe to re-write history as much as you like.<\/p>\n\n\n\n<h4>Conclusion<\/h4>\n\n\n\n<p>Both merge and rebase can be used to combine two branches. Merge command just unify your work with a commit without changing history. While rebase apply feature branch changes on top of master branch and change the history. If you prefer to have clean history, then you can use rebase. If you need to preserve the history changes, then merge would be the best choice.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>References:<\/p>\n\n\n\n<p><a href=\"https:\/\/www.atlassian.com\/de\/git\/tutorials\/merging-vs-rebasing\">https:\/\/www.atlassian.com\/de\/git\/tutorials\/merging-vs-rebasing<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/dev.to\/dhanushkadev\/\">https:\/\/dev.to\/dhanushkadev\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you are using git, most probably you may used git merge or git rebase to combine two branches. Pretty much both of these commands do the same thing, but in a different way. This post is to compare the difference between git merge vs rebase and when to use. Think about what happens if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":719,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[60],"tags":[61,151,153],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/718"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=718"}],"version-history":[{"count":5,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/718\/revisions"}],"predecessor-version":[{"id":2114,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/718\/revisions\/2114"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/719"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}