<?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: koshal garg</title>
    <description>The latest articles on DEV Community by koshal garg (@koshal_kkg).</description>
    <link>https://dev.to/koshal_kkg</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%2F394788%2F7784e053-de36-49da-aaf4-6060dc944f38.jpg</url>
      <title>DEV Community: koshal garg</title>
      <link>https://dev.to/koshal_kkg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/koshal_kkg"/>
    <language>en</language>
    <item>
      <title>Git Merge vs Rebase</title>
      <dc:creator>koshal garg</dc:creator>
      <pubDate>Mon, 25 May 2020 09:04:57 +0000</pubDate>
      <link>https://dev.to/koshal_kkg/git-merge-vs-rebase-3i5g</link>
      <guid>https://dev.to/koshal_kkg/git-merge-vs-rebase-3i5g</guid>
      <description>&lt;h1&gt;
  
  
  Git Merge vs Rebase
&lt;/h1&gt;

&lt;p&gt;Suppose in master branch we have 2 commits "m1" and "m2" and there is a feature branch from this commit. In feature branch we have new commits "f1" and "f2" but meanwhile master has new commit "m3". These are following ways to merge feature branch to master.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code to create initial commits
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init;
touch master.txt;

printf "m1" &amp;gt;&amp;gt;  master.txt ;
git add .;
git commit -m "m1";

printf "m2" &amp;gt;&amp;gt;  master.txt ;
git add .;
git commit -m "m2";

git checkout -b feature;
touch feature.txt;

printf "f1" &amp;gt;&amp;gt;  feature.txt ;
git add .;
git commit -m "f1";

git checkout master;
printf "m3" &amp;gt;&amp;gt;  master.txt ;
git add .;
git commit -m "m3";

git checkout feature;

printf "f2" &amp;gt;&amp;gt;  feature.txt ;
git add .;
git commit -m "f2";


# master: m1---&amp;gt;m2---&amp;gt;m3
# feature:       m2---&amp;gt;f1---&amp;gt;f2


&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Different ways of merging
&lt;/h2&gt;

&lt;h3&gt;
  
  
  git merge feature
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout master
git merge feature
# master: m1---&amp;gt;m2---&amp;gt;f1---&amp;gt;m3---&amp;gt;f2---&amp;gt;merge_commit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will create a new merge commit combining all the changes in master and feature&lt;/p&gt;

&lt;h3&gt;
  
  
  git merge --squash feature
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout master
git merge --squash feature
git add .;
git commit -m "merging feature";
# master: m1---&amp;gt;m2---&amp;gt;m3---&amp;gt;merging feature
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will squash all changes &lt;/p&gt;

&lt;h3&gt;
  
  
  git rebase master
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout feature
git rebase master
git checkout master;
git rebase feature;
# feature: m1---&amp;gt;m2---&amp;gt;m3---&amp;gt;f1---&amp;gt;f2
# master: m1---&amp;gt;m2---&amp;gt;m3---&amp;gt;f1---&amp;gt;f2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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