<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: GODORI</title>
    <description>The latest articles on DEV Community by GODORI (@godori).</description>
    <link>https://dev.to/godori</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F153059%2F6c831a86-3258-4a0a-8d3e-e42e14789bf5.png</url>
      <title>DEV Community: GODORI</title>
      <link>https://dev.to/godori</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/godori"/>
    <language>en</language>
    <item>
      <title>Rebasing a Branch in Git</title>
      <dc:creator>GODORI</dc:creator>
      <pubDate>Sun, 25 Aug 2019 10:52:27 +0000</pubDate>
      <link>https://dev.to/godori/rebasing-a-branch-in-git-3bc</link>
      <guid>https://dev.to/godori/rebasing-a-branch-in-git-3bc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;In this post you'll learn how to use Rebase to combine two branches.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Integrating Branches In Git
&lt;/h2&gt;

&lt;p&gt;If you have two diverging branches and want to integrate from one into another, you would typically accomplish that in two ways in Git.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Merge&lt;/li&gt;
&lt;li&gt;Rebase&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is pretty clear what &lt;code&gt;merge&lt;/code&gt; does from its name. But what in the world  &lt;code&gt;rebase&lt;/code&gt; means and why do you need it? Before you jump into &lt;code&gt;rebase&lt;/code&gt;, let's take a look at how &lt;code&gt;merge&lt;/code&gt; works first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Merge
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;merge&lt;/code&gt; command performs a &lt;code&gt;three-way merge&lt;/code&gt; between the two latest branch commits and base commit which is the most recent common ancestor of the two branches.&lt;/p&gt;

&lt;p&gt;For example below, there are two branches: a "feature" branch with a few commits and "master" branch.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fbfb81f20-b147-11e9-a131-ff9a59280693%2Fgit-branches.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fbfb81f20-b147-11e9-a131-ff9a59280693%2Fgit-branches.png" alt="git-branches.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Assume that the last commit of each feature and master is &lt;code&gt;f2&lt;/code&gt; and &lt;code&gt;m2&lt;/code&gt;, and there is &lt;code&gt;b&lt;/code&gt; which is the common ancestor of the two branches. Therefore you can create new commit based on those three commits.&lt;/p&gt;

&lt;p&gt;Okay, so what actually is the process of &lt;code&gt;three-way merge&lt;/code&gt; then? What would happen if you were doing a &lt;code&gt;two-way merge&lt;/code&gt;?&lt;/p&gt;

&lt;h3&gt;
  
  
  3-way merge
&lt;/h3&gt;

&lt;p&gt;Let's see why the 3-way merge is more efficient.&lt;/p&gt;

&lt;p&gt;Note that each a, b, c, d is part that is modified from the base commit which is the common ancestor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fcc6dd5f0-b149-11e9-9a09-5b90e5f36c66%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fcc6dd5f0-b149-11e9-9a09-5b90e5f36c66%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following table shows the modified part in &lt;code&gt;My&lt;/code&gt; branch and the &lt;code&gt;Other&lt;/code&gt; branch.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fba58aa60-b14a-11e9-a131-ff9a59280693%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fba58aa60-b14a-11e9-a131-ff9a59280693%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, &lt;code&gt;a&lt;/code&gt; hasn't changed in my branch but other modified it. Secondly, &lt;code&gt;b&lt;/code&gt; is unchanged on both sides. &lt;code&gt;c&lt;/code&gt; has changed differently. Lastly &lt;code&gt;d&lt;/code&gt; 's only changed on my branch.&lt;/p&gt;

&lt;p&gt;Let's say that we will merge comparing only two branches without common ancestor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fa2ba1b40-b14b-11e9-a131-ff9a59280693%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fa2ba1b40-b14b-11e9-a131-ff9a59280693%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But it's hard to know if conflict happens except the part b which is observed same on both sides.&lt;/p&gt;

&lt;p&gt;For this reason, we use the 3-way merge including the base commit during comparison to decide status of merge commit more clearly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F2e3c85e0-b14c-11e9-9a09-5b90e5f36c66%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F2e3c85e0-b14c-11e9-9a09-5b90e5f36c66%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In short, when merging two commit into one, we use the last commits of each branches and the latest common ancestor of the them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rebase
&lt;/h2&gt;

&lt;p&gt;Rebasing is the second way to combine multiple branches into one, which changes base the common ancestor to the different commit point. Let's have a look at the picture of branches again.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fa27c8b20-b14d-11e9-a131-ff9a59280693%2Fgit-branches.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fa27c8b20-b14d-11e9-a131-ff9a59280693%2Fgit-branches.png" alt="git-branches.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we want to see using rebase is like this!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fbf044d70-b2d1-11e9-a332-d525eafa8638%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fbf044d70-b2d1-11e9-a332-d525eafa8638%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In other words, you did re-basing the feature's base commit to &lt;code&gt;m2&lt;/code&gt; not &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here's the basic strategy of &lt;code&gt;rebase&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First off, git take patch of the changes from the commits and save it somewhere. Then it applies those to the master branch to create the new commits.&lt;/p&gt;

&lt;p&gt;Rebasing feature to master branch goes through a series of steps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Checkout to the feature branch
2. Rebase into the master branch
3. Fast-Forward Merge: feature branch into the master branch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F6e494040-b15d-11e9-8bdc-2f7c5d6ac42c%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F6e494040-b15d-11e9-8bdc-2f7c5d6ac42c%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why we need a &lt;code&gt;merge&lt;/code&gt; while rebasing? Take a look at &lt;a href="https://www.youtube.com/watch?v=_4JdLEhxlBQ" rel="noopener noreferrer"&gt;fast-forward merge&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's a step-by-step guide to rebasing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the checkout, HEAD points to the feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F174abe40-b15d-11e9-a131-ff9a59280693%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F174abe40-b15d-11e9-a131-ff9a59280693%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git rebase master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Git stores the differences(&lt;code&gt;▵1&lt;/code&gt;, &lt;code&gt;▵2&lt;/code&gt;) from the base commit of master and feature to the commit of current branches.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F8fa81e80-b15f-11e9-a308-131479da2b82%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F8fa81e80-b15f-11e9-a308-131479da2b82%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3
&lt;/h3&gt;

&lt;p&gt;Make HEAD point to the master&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F8b3a3d10-b15f-11e9-a308-131479da2b82%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F8b3a3d10-b15f-11e9-a308-131479da2b82%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Applying f1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, commit &lt;code&gt;f1&lt;/code&gt; is created by applying the &lt;code&gt;▵1&lt;/code&gt; into the &lt;code&gt;m2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fade9a210-b15f-11e9-9a9a-0f3d00cfbaf3%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fade9a210-b15f-11e9-9a9a-0f3d00cfbaf3%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Applying f2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a new commit &lt;code&gt;f2'&lt;/code&gt; applying &lt;code&gt;▵2&lt;/code&gt; diff on &lt;code&gt;f1'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Ff4617d30-b15f-11e9-9a9a-0f3d00cfbaf3%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Ff4617d30-b15f-11e9-9a9a-0f3d00cfbaf3%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6
&lt;/h3&gt;

&lt;p&gt;Now make &lt;code&gt;feature&lt;/code&gt; branch points &lt;code&gt;f2'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F2429d990-b160-11e9-9a9a-0f3d00cfbaf3%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2F2429d990-b160-11e9-9a9a-0f3d00cfbaf3%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git merge feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A rebase does not move the position of the master. You are able to complete all the process with a &lt;code&gt;fast-forward merge&lt;/code&gt; after rebasing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fb9224b40-b160-11e9-9a9a-0f3d00cfbaf3%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.velog.io%2Fpost-images%2Fgodori%2Fb9224b40-b160-11e9-9a9a-0f3d00cfbaf3%2Fimage.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Merge vs Rebase
&lt;/h2&gt;

&lt;p&gt;Which is better for combining two branches? It depends on the situation of your team and the project.&lt;/p&gt;

&lt;p&gt;If you want to remain all the commits including changing history, you can use the &lt;code&gt;merge&lt;/code&gt;. On the other hands if you want to keep a linear history without any merge commit, you should choose the &lt;code&gt;rebase&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/book/en/v2/Git-Branching-Rebasing" rel="noopener noreferrer"&gt;Pro Git - Rebasing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>rebase</category>
      <category>branch</category>
    </item>
  </channel>
</rss>
