<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ben Darlow on Medium]]></title>
        <description><![CDATA[Stories by Ben Darlow on Medium]]></description>
        <link>https://medium.com/@kapowaz?source=rss-ae681502287f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5fJd4yeqHrCtZ05V5qeZuw.png</url>
            <title>Stories by Ben Darlow on Medium</title>
            <link>https://medium.com/@kapowaz?source=rss-ae681502287f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 03:19:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kapowaz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Reset, Rebase Workflow]]></title>
            <link>https://medium.com/deliveroo-engineering/reset-rebase-workflow-33ca2a0767ab?source=rss-ae681502287f------2</link>
            <guid isPermaLink="false">https://medium.com/p/33ca2a0767ab</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Ben Darlow]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 11:49:52 GMT</pubDate>
            <atom:updated>2017-10-04T13:11:46.338Z</atom:updated>
            <content:encoded><![CDATA[<p>Rebase is one of git’s most powerful features. Most-commonly used to rewrite history by squashing related commits, and for keeping your branch up to date with master (without introducing unrelated merge commits), it can also facilitate some pretty clever workflow tricks which when used judiciously can let you factor out parts of your work into separate pull requests.</p><p>Small, atomic commits are the gold-standard for a nice, clean git commit history. If you embrace this ideology in your working practice you may find yourself committing frequently, but whilst you’re in the early stages of developing a feature it’s quite possible you’ll end up making decisions that you reverse-course on, or tweak as the concept solidifies.</p><p>Potentially this can mean you end up with a commit history containing many small ‘work in progress’ commits which you don’t necessarily want to merge into master in their current form. Sometimes you’ll make a change, then decide to solve a problem an entirely different way, undoing that change later before your feature branch is even merged. You may also end up writing code that — whilst it <em>supports</em> your work — has no direct requirement that it be <em>part of</em> the feature you’re building.</p><p>Keeping those commits in your feature branch — to be merged at the point that feature is ‘done’ — might make sense, but it might make <em>even more</em> sense to split them out into their own separate pull request, and there’s a workflow I’ve been using for a while to do just that. But in order to benefit from this workflow, we need to step through the dark, scary door marked git rebase…</p><h4>If we are to be prepared for it, we must first shed our fear of it</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kNB9_tVI5-SelXfZ4PqSww.jpeg" /><figcaption>I stand here, before you now, truthfully unafraid</figcaption></figure><p>To paraphrase <a href="https://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/">Angus Croll</a>, <em>you can’t master a tool until you know it inside out — and fear and evasion are the enemies of knowledge</em>, so I encourage you <em>not</em> to be <a href="https://nathanleclaire.com/blog/2014/09/14/dont-be-scared-of-git-rebase/">scared of git rebase</a>! There are numerous benefits to the workflow I’m about to describe, which you too can enjoy by conquering your fear of git rebase:</p><ul><li><a href="https://deliveroo.engineering/2017/09/06/play-pull-request-roulette.html">They’re easier to review (and <em>get</em> reviewed!)</a></li><li>They make conflicts less likely, and easier to fix when they happen</li><li>If you need to revert a change, it’s easier to do this, and there are fewer side-effects</li><li>You can reduce the cognitive overhead by focusing on the problem you’re <em>really</em> trying to solve</li><li>The newly-separated feature is available earlier, and potentially could benefit your co-workers or users.</li></ul><p>Focusing on that last one for a moment, let’s say that we have a pull request for a branch called new-widget-search, which contains our shiny new widget search page. In amongst that code is the first implementation of the new widget, and it’s working pretty well! There’s no reason that this feature can’t be merged into master (Continuous Integration permitting), and as it happens your co-worker Alison is nearly ready to start working on the new widget <em>editor</em>. Let’s kill two birds with one stone by unblocking her and simultaneously making our pull request smaller.</p><h4>Branch, Rebase, Reset…</h4><p>First of all, let’s pull master down and rebase on top of it, so that we can be certain we’re not missing anything important that’s been merged into master in the time since the work in the new-widget-search branch began. Then, to make sure that nothing we do here is going to affect our existing branch, we can create a <em>new branch</em> off of it:</p><pre>$ git checkout master<br>$ git pull<br>$ git checkout new-widget-search<br>$ git rebase master<br>$ git checkout -b new-widget</pre><p>Knowing that this branch only contains commits <em>ahead</em> of master, we can then run this:</p><pre>$ git reset master</pre><p>…which <em>removes</em> all commits in the branch, points HEAD to the tip of master, <em>but</em> most importantly <strong>leaves all changes from </strong><strong>new-widget-searchas unstaged changes</strong>. One of the great things about this is that all the intermediary states of the files that changed are squashed into their final resulting state:</p><pre>$ git status -s<br> M app/assets/stylesheets/application.scss<br> M app/controllers/widgets_controller.rb<br>?? app/assets/stylesheets/components/_new-widget-search.scss<br>?? app/assets/stylesheets/components/_new-widget.scss<br>?? app/views/components/new-widget-search-page.jsx<br>?? app/views/components/new-widget-search.jsx<br>?? app/views/components/new-widget.jsx</pre><p>Here we can see the changes we’ve been making as a bunch of new files, plus a couple of modifications to existing source files. Since we want this pull request to comprise <em>just</em> the functionality related to the new-widget component, we’ll now want to selectively stage some of these changes, commit them, and discard the rest. The easy bit is to add the new files we want:</p><pre>$ git add app/views/components/new-widget.jsx<br>$ git add app/assets/stylesheets/components/_new-widget.scss</pre><p>The changes to application.scss and widgets_controller.rb are a little trickier, since both of these contain new code just related to new-widget, <em>as well as</em> new code related to the larger new-widget-search feature branch which we <em>don’t</em> want to commit here. What we need to do is <em>selectively stage</em> part of the commit. This is a lot easier to do if you have a GUI git client (I use <a href="https://www.git-tower.com/">Git Tower</a>) but you can do it from the command line too:</p><pre>$ git add -p app/assets/stylesheets/application.scss</pre><p>This will prompt you to create a patch:</p><pre>diff --git a/application.scss b/application.scss<br>index 0b91044..1183059 100644<br>--- a/application.scss<br>+++ b/application.scss<br>@@ -8,6 +8,8 @@<br>// Components<br>@import &#39;components/buttons&#39;;<br>@import &#39;components/modal&#39;;<br>+@import &#39;components/new-widget&#39;;<br>+@import &#39;components/new-widget-search&#39;;</pre><pre>// Pages<br>@import &#39;pages/home&#39;;<br>Stage this hunk [y,n,q,a,d,/,e,?]?</pre><p>Since we don’t want to include the entirety of this hunk — just the line that adds components/new-widget — we’ll want to edit this interactively, so press e to launch the diff editor, where you’ll see something like this:</p><pre># Manual hunk edit mode -- see bottom for a quick guide<br>@@ -8,6 +8,8 @@<br>// Components<br>@import &#39;components/buttons&#39;;<br>@import &#39;components/modal&#39;;<br>+@import &#39;components/new-widget&#39;;<br>+@import &#39;components/new-widget-search&#39;;</pre><pre>// Pages<br>@import &#39;pages/home&#39;;<br># ---<br># To remove &#39;-&#39; lines, make them &#39; &#39; lines (context).<br># To remove &#39;+&#39; lines, delete them.<br># Lines starting with # will be removed.<br>#<br># If the patch applies cleanly, the edited hunk will immediately be<br># marked for staging. If it does not apply cleanly, you will be given<br># an opportunity to edit again. If all lines of the hunk are removed,<br># then the edit is aborted and the hunk is left unchanged.</pre><p>To unstage the change which adds the reference to &#39;components/new-widget-search&#39;, simply delete that whole line, then save and close the file. Repeating the same process with widgets_controller.rb we now see the following output when we run git status -s:</p><pre>$ git status -s<br>MM app/assets/stylesheets/application.scss<br>A  app/assets/stylesheets/components/_new-widget.scss<br>?? app/assets/stylesheets/components/_new-widget-search.scss<br>MM app/controllers/widgets_controller.rb<br>?? app/views/components/new-widget-search.jsx<br>A  app/views/components/new-widget.jsx<br>?? app/views/containers/new-widget-search-page.jsx</pre><p>We can now safely create a commit from <em>just</em> the staged changes:</p><pre>$ git commit -m &quot;Add new-widget component&quot;</pre><p>Once you’re done, you can push your new branch, create another pull request and kick off the review process to get it merged into master! Now might be a good point to let Alison know that the new-widget component is ready to go, so she can start building her own feature that makes use of it.</p><h4>Committing with hindsight</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*21EREJ8MaYduMaeX435ZLA.jpeg" /><figcaption>Oh man, it’s a scenario <strong><em>four</em></strong>.</figcaption></figure><p>Once you’ve done all of that, you <em>could</em> choose to discard the unstaged changes you’re left over with and then rebase your original branch of top of master. If you did, all of the changes related to new-widget will magically disappear from your branch, making your PR smaller. However, if you were building your feature branch from many ‘work-in-progress’ commits, you might also want to take this opportunity to do some tidying up.</p><p>For any given non-trivial problem, your understanding of the problem will almost always be a lot more coherent at <em>the end</em> of the process than it was at the beginning. This is inevitably reflected in the commits your solution is comprised of, even if the final outcome was far more elegant and effective than it may have been to begin with. Ideally we’d be able to benefit from hindsight from the beginning of our work, and luckily that’s something that the <em>reset-rebase</em> workflow gives us!</p><p>Let’s assume that your work on the main new-widget-search feature is pretty close to complete; after a few rounds of revision and refactoring the branch is looking good. It’s just a shame about the many messy WIP and Fix typo commits! Luckily the unstaged changes we now have in our working copy give us the perfect opportunity to create some tidy, feature-specific commits. And since all the changes have been squashed already, we don’t need to worry about conflict resolution from intermediary stages of development.</p><p>Following the same process as above, create a new branch from the new-widget branch you created earlier:</p><pre>$ git checkout -b new-widget-search-rebased new-widget</pre><p>At this point, using exactly the same procedure as before, we can start creating a series of <em>structured, atomic commits</em> that reflect units of related work, by staging new files and relevant lines of <em>new code</em> from existing files. Once all that’s done, a quick check of the history should reveals this much cleaner series of commits:</p><pre>$ git log<br>commit 46aa1b9bdda9e9d96f978381503a983e9e890bad<br>Author: Ben Darlow &lt;ob.fusc@t.ed&gt;<br>Date:   Thu Sep 7 16:31:26 2017 +0100</pre><pre>    Implement New Widget Search page</pre><pre>commit 77407db02c62df3326a0f380818457aadbc3c1ca<br>Author: Ben Darlow &lt;ob.fusc@t.ed&gt;<br>Date:   Thu Sep 7 16:15:58 2017 +0100</pre><pre>    Implement new widget</pre><pre>commit 707548c16a44f629584fc4bae93b8014888c19a8<br>Author: Ben Darlow &lt;ob.fusc@t.ed&gt;<br>Date:   Thu Sep 7 14:29:04 2017 +0100</pre><pre>    Initial commit</pre><p>Once you’re happy with the state of this new feature branch, you can safely delete the old branch, and rename the newer branch to new-widget-search in its place:</p><pre>$ git branch --delete --force new-widget-search<br>$ git branch --move new-widget-search-rebased new-widget-search</pre><p>If you already pushed the branch, you’ll also want to ensure you set the remote URL for this newer branch to the existing one, then <em>force push</em> to overwrite the old branch. Of course, all the usual caveats about using force push apply: if anybody else is working on this branch too, make sure you talk to them before you force push and wipe out their changes!</p><pre>$ git branch --set-upstream new-widget-search origin/new-widget-search<br>$ git push --force origin new-widget-search</pre><p>After this, you should be in a position to create another, new pull request based on your actual feature, which should be tidier, smaller and — with a bit of luck — easier to review!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33ca2a0767ab" width="1" height="1" alt=""><hr><p><a href="https://medium.com/deliveroo-engineering/reset-rebase-workflow-33ca2a0767ab">Reset, Rebase Workflow</a> was originally published in <a href="https://medium.com/deliveroo-engineering">Deliveroo Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>