<?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>Forem: Alex Ferreira</title>
    <description>The latest articles on Forem by Alex Ferreira (@mral_x).</description>
    <link>https://forem.com/mral_x</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%2F152841%2F42b10862-bc62-4897-9e54-c4c4061e2db8.JPG</url>
      <title>Forem: Alex Ferreira</title>
      <link>https://forem.com/mral_x</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mral_x"/>
    <language>en</language>
    <item>
      <title>Git Rebase and Interactive Rebase</title>
      <dc:creator>Alex Ferreira</dc:creator>
      <pubDate>Thu, 29 Apr 2021 18:06:29 +0000</pubDate>
      <link>https://forem.com/mral_x/git-rebase-and-interactive-rebase-6e2</link>
      <guid>https://forem.com/mral_x/git-rebase-and-interactive-rebase-6e2</guid>
      <description>&lt;h2&gt;
  
  
  What is a rebase? 📚
&lt;/h2&gt;

&lt;p&gt;Rebasing is one of the two Git process of integrating changes from one branch to another.&lt;/p&gt;

&lt;p&gt;While &lt;code&gt;merging&lt;/code&gt; always moves a branch forward in history by simply adding a new commit representing each branch integration, &lt;code&gt;rebasing&lt;/code&gt; can be much more powerful (and dangerous) as it allows us to rewrite history.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhhquqa3hv8ryyay1ukw6.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhhquqa3hv8ryyay1ukw6.jpg" alt="1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we rebase? 🧐
&lt;/h2&gt;

&lt;p&gt;Rebasing is the process of &lt;code&gt;moving&lt;/code&gt; or &lt;code&gt;combining&lt;/code&gt;(i.e. squashing) a &lt;strong&gt;sequence of commits&lt;/strong&gt; to a new base commit.&lt;/p&gt;

&lt;p&gt;Imagine you're working on a feature branch and after sometime you realise the &lt;code&gt;main&lt;/code&gt; (aka "master") branch has been updated by someone else's code. This means your branch has now diverged from the &lt;code&gt;main&lt;/code&gt; branch. At some point you will want to include said changes in your feature branch. A common way of doing this would be to simply do a &lt;code&gt;git pull&lt;/code&gt; from &lt;code&gt;main&lt;/code&gt; which would add a &lt;em&gt;merge commit&lt;/em&gt; to your feature branch.&lt;/p&gt;

&lt;p&gt;The issues with this method can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Merge commits&lt;/code&gt; can be very vague, providing very little information in the git tree which makes debugging a much harder task&lt;/li&gt;
&lt;li&gt;Updating a branch - by &lt;code&gt;pulling&lt;/code&gt;/&lt;code&gt;merging&lt;/code&gt; - multiple times can result in a git history similar to this:&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1bjbide7wlmds1z1xhgr.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1bjbide7wlmds1z1xhgr.png" alt="2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reading this history provides very little (if any) information on what changes have been made to the project&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The goal of rebasing is to keep a clean, clear project history. So instead of pulling the &lt;code&gt;main&lt;/code&gt; changes and add a merge commit we can tell our branch to move its commits to the top of the latest changes &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frf2gl1srlyiksxvbws3z.gif" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frf2gl1srlyiksxvbws3z.gif" alt="3"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Feature branch &lt;em&gt;JIRA-123&lt;/em&gt; is being rebased to the top of the master branch&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By rebasing our feature branch to the top of the &lt;code&gt;main&lt;/code&gt; branch, we keep all the changes and commits we made unaltered while getting the latest main &lt;code&gt;updates&lt;/code&gt;. In order to achieve this we can execute the following command &lt;strong&gt;while in our feature branch&lt;/strong&gt;:&lt;/p&gt;

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

$ git rebase main


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

&lt;/div&gt;

&lt;p&gt;This will start the rebase process. If no conflicts are found you should see a success message and your feature branch is now up-to-date with the &lt;code&gt;main&lt;/code&gt; branch and we're one step closer to having a project history similar to 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ozduj2c0es01wdegc05.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ozduj2c0es01wdegc05.png" alt="4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Interactive rebase 🔬
&lt;/h2&gt;

&lt;p&gt;Interactive rebasing brings the power of rebasing to a whole new level! It allows us to interactively change multiple commits in different ways.&lt;/p&gt;

&lt;p&gt;You can start an interactive rebase by executing a rebase command followed by the &lt;code&gt;-i&lt;/code&gt; parameter and the commit we want to modify:&lt;/p&gt;

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

$ git rebase -i &amp;lt;commit hash | HEAD position&amp;gt;


&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd0tz0ga2upmvt6rjrmbf.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd0tz0ga2upmvt6rjrmbf.png" alt="5"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Interactive rebase CLI showing the three last commits and always in order from oldest (top) to newest (bottom)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can tell just by looking at list of commands displayed in the CLI how powerful this tool can be! We can edit commits, squash them, remove them, amend them and so much more. We can even permanently change the commit order!&lt;/p&gt;

&lt;h3&gt;
  
  
  Squashing
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Squashing&lt;/code&gt; allows us to specify which commits we want to combine in order to help maintain clean history.&lt;/p&gt;

&lt;p&gt;Does something like this look familiar?&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67mfzafphrok40jxkk2z.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67mfzafphrok40jxkk2z.png" alt="6"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Well, if you like committing a lot while you work you probably don't like thinking about being super descriptive on every single commit message. Squashing can be extremely helpful in these situations because starting an interactive rebase you can then tell git which commits will be "merged" into one (or more) and then edit its commit message.&lt;/p&gt;

&lt;p&gt;Keep in mind that we must always squash a commit "into" another one.&lt;/p&gt;

&lt;p&gt;So say we have 6 commits and we think only two of them are relevant we could squash the other 4 like:&lt;/p&gt;

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

$ git rebase -i HEAD~6


&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fukbt428ugvebgwj4w763.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fukbt428ugvebgwj4w763.png" alt="7"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"s" is simply a shorthand name for "squash"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this case we're saying we'd like to merge commits 2 - 5 and include them in the first one (&lt;code&gt;77497f5&lt;/code&gt;) and keep the last commit as is.&lt;/p&gt;

&lt;p&gt;Next git will rebase each commit to their new base (&lt;code&gt;77497f5&lt;/code&gt;) and if there are no conflicts, you'll be able to change their final commit messages and voila! Only 2 commits total!&lt;/p&gt;

&lt;h2&gt;
  
  
  What could go wrong? 😇
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Conflicts
&lt;/h3&gt;

&lt;p&gt;Similarly to what happens with a &lt;code&gt;git merge&lt;/code&gt;, updating a branch with another can result in code conflicts.&lt;/p&gt;

&lt;p&gt;A big difference of rebase conflicts is that we're not dealing with one &lt;code&gt;merge commit&lt;/code&gt; but with (possibly) multiple commits being moved.&lt;/p&gt;

&lt;p&gt;This means rebase conflict solving is an iterative process that goes through each commit being rebased.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7oquxpopbqx4uh6jhe8.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7oquxpopbqx4uh6jhe8.png" alt="8"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a feature branch with two commits, if a conflict is found while rebasing &lt;em&gt;commit A&lt;/em&gt; we fix it, tell git to continue the process to the next commit, fix the new conflicts on &lt;em&gt;commit B&lt;/em&gt; ****and only then we'll be done&lt;/p&gt;

&lt;h3&gt;
  
  
  Pushing local branch after rebasing
&lt;/h3&gt;

&lt;p&gt;Rebasing a local feature branch means &lt;strong&gt;we're rewriting history&lt;/strong&gt;, so what this actually means is that we're not cherry picking the feature branch commits to the new base. Although visually everything looks the same Git creates new commits (with new hashes) on the targeted branch.&lt;/p&gt;

&lt;p&gt;This means whenever we try simply pushing our changes to our remote repo we get an error stating the local and remote branches have diverged.&lt;/p&gt;

&lt;p&gt;To fix this problem it's good practice to &lt;code&gt;push&lt;/code&gt; using the &lt;code&gt;—-force-with-lease&lt;/code&gt; parameter.&lt;/p&gt;

&lt;p&gt;You can check &lt;a href="https://blog.developer.atlassian.com/force-with-lease/" rel="noopener noreferrer"&gt;—force vs —force with lease&lt;/a&gt; to learn more about this.&lt;/p&gt;

&lt;h3&gt;
  
  
  "I think I made a mistake"
&lt;/h3&gt;

&lt;p&gt;Have you heard of the beautiful command:&lt;/p&gt;

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

$ git reflog


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

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;Reflog&lt;/code&gt; allows us to check every change we did to our local branch. Running the command will return something 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsae8eox6to9gpjotv3xa.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsae8eox6to9gpjotv3xa.png" alt="9"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reflog output showing the last 3 actions&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this case if I'd like to go back before I ever did the rebase I could jump back to &lt;code&gt;HEAD~2&lt;/code&gt; or using its hash instead:&lt;/p&gt;

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

$ git checkout 682c215


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

&lt;/div&gt;

&lt;p&gt;At this point I'm are in the past and can undo the mistakes I'd made before. Time travel!! 🏎🔥&lt;/p&gt;

&lt;h3&gt;
  
  
  If things get out of control
&lt;/h3&gt;

&lt;p&gt;At any point in our interactive rebase if things get too messy and we no longer know how to fix them, we can always abort the whole process in the terminal and the interactive rebase will be canceled completely:&lt;/p&gt;


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

&lt;p&gt;$ git rebase --abort&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;As you can tell rebase can be used for a lot of different purposes, especially using the interactive rebase functionality. Hopefully this article provides you with just enough to get started messing around with it. &lt;/p&gt;

&lt;p&gt;Don't worry if you run into issues understanding some of the nuances of git rebase. It has its learning curve and that's why I included some escape methods so you can go back and try again 💪&lt;/p&gt;

</description>
      <category>git</category>
      <category>rebase</category>
      <category>rebasing</category>
      <category>merge</category>
    </item>
  </channel>
</rss>
