<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Git | Alex Ho</title>
    <link>/tags/git/</link>
      <atom:link href="/tags/git/index.xml" rel="self" type="application/rss+xml" />
    <description>Git</description>
    <generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-gb</language><lastBuildDate>Sat, 08 Oct 2022 16:00:00 +0000</lastBuildDate>
    <image>
      <url>/media/icon_hu_13d0a7a216a183f.png</url>
      <title>Git</title>
      <link>/tags/git/</link>
    </image>
    
    <item>
      <title>Git rebase with dependent branches</title>
      <link>/post/git-rebase-update-refs/</link>
      <pubDate>Sat, 08 Oct 2022 16:00:00 +0000</pubDate>
      <guid>/post/git-rebase-update-refs/</guid>
      <description>&lt;p&gt;&lt;code&gt;git rebase&lt;/code&gt; is always an important part of my workflow. As a maintainer of
repositories at work, browsing commit history of a major part of my work. Thus,
keeping the history &amp;ldquo;clean&amp;rdquo; is important. By &amp;ldquo;clean&amp;rdquo;, it means&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;code can be compiled and unit tests passes, and&lt;/li&gt;
&lt;li&gt;code changes of a commit is complete and can be understood as a whole (not
necessarily a complete feature but completeness on code level)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;During active development in a feature branch, I prefer to commit small and
often. This creates a lot of commits which it useful for me to understand the
trail and to change the ordering of the commits. However, these small commits
are probably only useful during active development but not so much when
debugging the same code a few months later. Thus, my practice is always to
rebase (in other words, re-writing the history of) the feature branch before
creating a pull request.&lt;/p&gt;
&lt;p&gt;Another reason for using rebase is to avoid any merge commits in a feature
branch. I understand this controversial and it also depends on team&amp;rsquo;s policy.
Personally, I do not see any use cases of having merge commits in a feature
branch and I think it is only acceptable in &lt;code&gt;main&lt;/code&gt; (or &lt;code&gt;master&lt;/code&gt;). Thus, I will
have a practice to rebase a feature branch before merging it into &lt;code&gt;main&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;There are times when development of a feature &lt;code&gt;feature-b&lt;/code&gt; depends on another
feature &lt;code&gt;feature-b&lt;/code&gt; which is also in development. In this case, I would branch
out from &lt;code&gt;feature-a&lt;/code&gt; to create &lt;code&gt;feature-b&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;















&lt;figure  &gt;
  &lt;div class=&#34;flex justify-center	&#34;&gt;
    &lt;div class=&#34;w-full&#34; &gt;&lt;img src=&#34;./images/original_feature_b.webp&#34; alt=&#34;original feature-b&#34; loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p&gt;This works fine until I want to patch one of the commits originated from
&lt;code&gt;feature-a&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;















&lt;figure  &gt;
  &lt;div class=&#34;flex justify-center	&#34;&gt;
    &lt;div class=&#34;w-full&#34; &gt;&lt;img src=&#34;./images/feature_b_with_fixup.webp&#34; alt=&#34;feature-b with fixup&#34; loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p&gt;In such case, I will probably have to checkout &lt;code&gt;feature-a&lt;/code&gt;, create a fixup
commit, rebase &lt;code&gt;feature-a&lt;/code&gt; to squash the fixup commit, checkout &lt;code&gt;feature-b&lt;/code&gt;
again, and rebase it against &lt;code&gt;feature-a&lt;/code&gt;. The following shows the trail of
commands involved. This is pretty involved.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-sh&#34; data-lang=&#34;sh&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;git checkout feature-a
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;vim code.cs
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;git add code.cs
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;git commit --fixup feature-a~
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;git rebase --autosquash -i master
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;git checkout feature-b
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;git rebase -i feature-a
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Thanks to 
,
such patch can be done directly on &lt;code&gt;feature-b&lt;/code&gt;. The trail of commands look like
the following.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-sh&#34; data-lang=&#34;sh&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;vim code.cs
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;git add code.cs
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;git commit --fixup feature-a~
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;git rebase --autosquash --update-refs -i master
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Since &lt;code&gt;feature-b&lt;/code&gt; has a reference of &lt;code&gt;feature-a&lt;/code&gt;, option &lt;code&gt;--update-refs&lt;/code&gt; will be
able to help rebasing &lt;code&gt;feature-a&lt;/code&gt; as well. This is very useful command when
dealing with multiple development branches and it could save me a lot of time!&lt;/p&gt;
&lt;p&gt;















&lt;figure  &gt;
  &lt;div class=&#34;flex justify-center	&#34;&gt;
    &lt;div class=&#34;w-full&#34; &gt;&lt;img src=&#34;./images/rebase_message.webp&#34; alt=&#34;rebase command and message&#34; loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p&gt;















&lt;figure  &gt;
  &lt;div class=&#34;flex justify-center	&#34;&gt;
    &lt;div class=&#34;w-full&#34; &gt;&lt;img src=&#34;./images/rebased_feature_b.webp&#34; alt=&#34;rebased feature-b&#34; loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p&gt;















&lt;figure  &gt;
  &lt;div class=&#34;flex justify-center	&#34;&gt;
    &lt;div class=&#34;w-full&#34; &gt;&lt;img src=&#34;./images/rebased_feature_a.webp&#34; alt=&#34;rebased feature-a&#34; loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p&gt;Hope this helps you as well.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
