<?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 Alexander Quine on Medium]]></title>
        <description><![CDATA[Stories by Alexander Quine on Medium]]></description>
        <link>https://medium.com/@drwxrxrx?source=rss-324e593949f1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Kr7AQSfpFLsBKn78Xn4P1Q.jpeg</url>
            <title>Stories by Alexander Quine on Medium</title>
            <link>https://medium.com/@drwxrxrx?source=rss-324e593949f1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 20:33:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@drwxrxrx/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[Working Remotely at B/R]]></title>
            <link>https://medium.com/bleacher-report-engineering/working-remotely-at-b-r-fac57de65810?source=rss-324e593949f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/fac57de65810</guid>
            <category><![CDATA[office-culture]]></category>
            <category><![CDATA[remote-working]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Alexander Quine]]></dc:creator>
            <pubDate>Fri, 24 May 2019 17:03:23 GMT</pubDate>
            <atom:updated>2019-05-24T17:06:03.062Z</atom:updated>
            <content:encoded><![CDATA[<h3>Working Remotely at Bleacher Report</h3><p>Ever wonder what it’s like to work at B/R from the comfort of your own home?</p><p>Joe McLaughlin, Functional Lead for iOS Engineering, detailed <a href="https://josephmclaughl.in/posts/working-remotely/">his experiences working remotely with B/R Engineering for the last three years</a>. Visit <a href="https://josephmclaughl.in/">his blog</a> and check it out!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qSkJ0bzk0nLWyioOCB5I6g.png" /><figcaption>The record player and vinyl are not part of our remote employee setup… (But maybe they should be??)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fac57de65810" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bleacher-report-engineering/working-remotely-at-b-r-fac57de65810">Working Remotely at B/R</a> was originally published in <a href="https://medium.com/bleacher-report-engineering">Bleacher Report Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Small Commits for Fun and Profit, part 2: git commit --amend and git rebase --interactive]]></title>
            <link>https://medium.com/bleacher-report-engineering/small-commits-for-fun-and-profit-part-2-git-commit-amend-and-git-rebase-interactive-e986d4a64c21?source=rss-324e593949f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/e986d4a64c21</guid>
            <category><![CDATA[code-review]]></category>
            <category><![CDATA[version-control]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Alexander Quine]]></dc:creator>
            <pubDate>Thu, 15 Feb 2018 22:08:18 GMT</pubDate>
            <atom:updated>2019-02-28T22:26:00.691Z</atom:updated>
            <content:encoded><![CDATA[<p>(<a href="http://medium.com/p/c0966a562b10">Check out Part 1, all about </a><a href="http://medium.com/p/c0966a562b10">git add --patch.</a>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*75GIyh005_Wl9vRQF3tgTQ.png" /></figure><p>It’s helpful for code reviewers (and future code archaeologists) to have a neatly-organized history of changes to your code. In Part 1 we looked at how git add --patch lets us be mindful about exactly what code will go into each commit. But what if you notice that you’ve made a mistake in a previous commit? It’s easy to create a new commit with a message like “fix typo” or “swap order of parameters” or whatever, and as long as that’s the only thing you’re doing it the commit, that seems Good Enough.</p><h4>git commit --amend</h4><p>If the commit which introduced the error happens to be the very last commit you made*, you’re in luck! We’re going to git add the typo fix, but instead of creating a normal new commit, we’ll use git commit --amend to “squish together” the last commit <em>plus</em> this typo-fix. This will bring up your editor, giving you the opportunity to modify the commit message now that you’ve fixed the typo. When you’re happy with the commit message, save and close your editor, and thengit log --patch -1 to see a diff of your now-typo-less commit.</p><p>* …and you haven’t pushed that last commit anywhere! See “A brief warning” section below…</p><h4>git rebase --interactive</h4><p>Unfortunately, I often discover an error or typo a few commits in the past. Fortunately, all is not lost! If I haven’t touched the code around that error-containing line*, git rebase --interactive is here to save me.</p><p>* it’s still possible to rebase like this if I <em>have</em> touched nearby code; it’s just a little more fiddly… that’ll be for a later blog post.</p><p>Okay, here’s how it works. To set the stage: I’m already working on a feature branch, where I can mess with git history with ̶i̶m̶p̶u̶n̶i̶t̶y̶ care and won’t botch the working environments of any of my colleagues (i.e., <strong>do not do this on shared branches!</strong>).</p><p>(As <a href="http://2ndscale.com/rtomayko/2008/the-thing-about-git">Ryan Tomayko so eloquently put it</a>, git rebase --interactive “is a bit like git commit --amend hopped up on acid and holding a chainsaw — completely insane and quite dangerous but capable of exposing entirely new states of mind.” This blog post is an attempt at explaining one of these new states of mind, while ignoring the insane and dangerous bits.)</p><p>I’ve already made commits A, B, and C, but when I look at my git history, I notice that I’ve made a crucial error in commit B (turns out, I can’t spell function). I can’t simply use git commit --amend because that would modify the most recent commit which is C, whereas I need to modify commit B.</p><p>In order to modify commit B, I’m first going to make a temporary commit D. This will contain <em>only</em> the correction which I want to end up in commit B. This is a normal commit: edit the file with the error, git add it (perhaps git add --patch if that file has other not-yet-committed modifications!), and git commit it with a helpful message (such as “correct funciton to function”).</p><p>Now I have Commits A, B, C, and D. My goal is to combine Commit D into Commit B, and end up with a single Commit B-prime, which replaces Commit B. Then my git history will have, in order: Commits A, B-prime, C (the temporary Commit D will be “gone”).</p><p>The interactive rebase command wants to know the <em>parent</em> of the earliest commit to modify. Because we want to modify back to and including Commit B, we will need to give the rebase command the SHA of Commit B’s <em>parent</em>, which is Commit A. (Use git log to identify the SHA of Commit A on your local repo; I will use “a1aaaaa” as an example.)</p><p>Grab your shell and run git rebase --interactive a1aaaaa and it should pop open your trusty editor again. It’s showing a list of the three commits between Commit A and HEAD, and asking what we’d like to do with them; after the list of commits, it lists what we can do for each commit: pick, reword, edit, squash, fixup, or exec. There are two more things we can do: remove a commit completely (by deleting the line referencing the commit), or reorder the commits (by swapping around the lines). (Are you starting to see where we’re heading here?)</p><p>The note for squash says that it will “use commit, but meld into previous commit”, which is <em>almost</em> what we want—we are trying to squash Commit D into Commit B, but B is not the <em>previous</em> commit. However, we can also reorder commits right here, so we can literally move Commit D to come <em>before</em> Commit C, whereupon squash becomes exactly what we want.</p><p>Here we go: swap the line referencing Commit C with the line referencing Commit D, so the order becomes (top to bottom) B, D, C.</p><p>Now that Commit D is immediately after Commit B, we can squash D right into B, thus creating a Commit B-prime which is a combination of the two—and thereby making it look like the typo never happened. The first word of each of these lines referencing a commit is initially pick, the default action which means “use this commit normally”. For Commit D, change that pick to squash, so that commit will be merged into the previous one (which is now Commit B).</p><p>With the order now B, D, C, and the line for Commit D starting with squash, save and close the editor. It’ll pop right back up again, but this time with the log messages for both Commit B and D. Now it’s asking you to create a new-and-improved commit message for the new-and-improved Commit B-prime. Tweak the message to your heart’s content, then save-and-quit the editor. You should see git printing out some progress notes as it shuffles things, and then once it’s done: “Successfully rebased and updated refs/heads/name-of-your-branch.”</p><p>(Pro tip: in this contrived typo-fix example, you’ll probably want to continue using the message from commit B, without noting that you had to delve into history to fix a typo. If that’s the case, use fixup: it’ll discard the message for Commit D entirely, and reuse Commit B’s message.)</p><h4>a brief warning</h4><p>What we’re doing here is called “rewriting history”, and that should sound dangerous. Git helps us share code between people, and part of its underlying social contract is that once the history for code has been “established”, it should stay that way. The primary reason for this is practical: when multiple people are working on the same chunk of code, a modification to the history of that code will propagate to all the people working on it — and when they don’t expect that modification, they get surprised and upset when the foundation they’re building upon has suddenly changed.</p><p>My corollary to this understanding is that branches which are not yet intended to be shared with other people (such as my still-in-development feature branch) <em>may</em> have their histories rewritten without surprising and upsetting my colleagues. Said another way: if I’m the only one depending on a piece of history, I can rewrite it and it won’t screw up my coworkers’ development.</p><p>So: <strong>Don’t rewrite <em>shared</em> history!</strong></p><h4>Wrapping things up</h4><p>If you’ve been following along, we’ve just “Successfully rebased” and our git log reflects that rather than our original commits A B C and D, we now have A B-prime and C. This worked smoothly because the mistake in commit B could be cleanly fixed by commit D and then combined together to create commit B-prime, without modifying the code which was changed in the intervening commit C.</p><p>This basic example illustrates the use of git rebase --interactive to modify the code associated with an existing commit. This is a powerful (almost magical) tool, so it must be used carefully — do not surprise your colleagues with an ever-changing Git log! — but it will give you the power to curate an easily-understood history of code changes.</p><p>That’s all for now!</p><p><em>Next in my Git Manifesto Series: Part 3 will show how to use </em><em>git cherry-pick to merge specific commits from other branches!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e986d4a64c21" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bleacher-report-engineering/small-commits-for-fun-and-profit-part-2-git-commit-amend-and-git-rebase-interactive-e986d4a64c21">Small Commits for Fun and Profit, part 2: git commit --amend and git rebase --interactive</a> was originally published in <a href="https://medium.com/bleacher-report-engineering">Bleacher Report Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Open-sourcing a few ESLint rules]]></title>
            <link>https://medium.com/bleacher-report-engineering/open-sourcing-a-few-eslint-rules-47b55e97e8cd?source=rss-324e593949f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/47b55e97e8cd</guid>
            <category><![CDATA[eslint]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alexander Quine]]></dc:creator>
            <pubDate>Mon, 03 Apr 2017 17:23:44 GMT</pubDate>
            <atom:updated>2017-04-03T17:24:44.749Z</atom:updated>
            <content:encoded><![CDATA[<p>The Engineering team at Bleacher Report writes a lot of JavaScript, and we’ve taken to using <a href="http://eslint.org/">ESLint</a> to analyze our code and warn us of potential issues or refactors. ESLint ships with lots of configurable rules but also makes it easy to write custom rules, so we’ve written a handful of our own. We’re happy to announce an open-source ESLint plugin containing these rules, which we’re calling <a href="https://github.com/bleacherreport/eslint-plugin-laws-of-the-game/">the Laws of the Game</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VK0drOHtCV7JB2DqsnwPdg.png" /><figcaption>lint-free JavaScript is happy like a lint-free Drake</figcaption></figure><p>It makes a few rules available, perhaps most notably prefer-includes-over-indexof, which will encourage you to use ES2016’s Array.prototype.includes function to determine whether an element occurs in an array (which is a little more expressive than the classic JavaScript approach of comparing the result of indexOf to -1).</p><p><a href="https://www.npmjs.com/package/@br/eslint-plugin-laws-of-the-game">The Laws of the Game plugin</a> is published in <a href="https://www.npmjs.com/org/br">Bleacher Report’s org on npm</a>, so it can be installed into an existing npm project by running npm install --save-dev @br/eslint-plugin-laws-of-the-game, and then adding it to your .eslintrc&#39;s “plugins” section and specifying which rules you’d like to enable. More info is available in <a href="https://github.com/bleacherreport/eslint-plugin-laws-of-the-game/blob/master/README.md">the README file</a>.</p><p>Happy linting!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47b55e97e8cd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bleacher-report-engineering/open-sourcing-a-few-eslint-rules-47b55e97e8cd">Open-sourcing a few ESLint rules</a> was originally published in <a href="https://medium.com/bleacher-report-engineering">Bleacher Report Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Small Commits for Fun and Profit, part 1: git add --patch]]></title>
            <link>https://medium.com/bleacher-report-engineering/small-commits-for-fun-and-profit-part-1-git-add-patch-c0966a562b10?source=rss-324e593949f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0966a562b10</guid>
            <category><![CDATA[code-review]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[version-control]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Alexander Quine]]></dc:creator>
            <pubDate>Thu, 09 Mar 2017 18:21:41 GMT</pubDate>
            <atom:updated>2019-02-28T22:27:12.499Z</atom:updated>
            <content:encoded><![CDATA[<p>Small commits don’t have to be a hassle to create, and they have loads of benefits: Your git history is easier to skim; they can make typos more obvious in code reviews; and, with a well-tended git history, one of git’s secret weapons can tell you where your bug regressions came from!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*RRAWy_etG4K1H-DSZ2W1fQ.png" /><figcaption>This series of posts is for you. (<a href="https://xkcd.com/1296/">xkcd’s “Git Commit”</a>)</figcaption></figure><p>I’ll start with my reasoning for liking smaller commits. If you’re already on board, jump to the git add --patch section, where I walk through the interactive way of choosing what will make it into a commit. In part 2 I’ll look at git commit --amend and git rebase --interactive, which let us rewrite commits and edit messages in order to curate an easy-to-follow git history. Once my git history reads like a book, my colleagues may want to include a commit or two of mine in some of their branches, so in part 3 I’ll look at git cherry-pick. Finally, in part 4 I’ll use git bisect to track down exactly which commit introduced a regression.</p><h3>Why small commits?</h3><p>When I’m reading or modifying code, I often find myself making changes that are theoretically unrelated to the work I’m <em>supposed</em> to be doing. These could be as minor as fixing a typo in a comment or more significant, such as modifying an interface. Either way, if the ticket I’m working on is asking me to change the format of a date, whoever reviews my code will be confused by seeing a diff that includes changes to interfaces in some seemingly unrelated corner of the codebase.</p><p>Even if I don’t end up tidying or refactoring anything as I’m getting some Real Work done, the bugfix or feature I’m implementing may have a couple of logical pieces. For example, displaying a new type of content on a page may require (1) some data-layer changes to let the system store a new shape of data, (2) some view-layer changes to generate some HTML for this new data and (3) some CSS or JavaScript changes to style the HTML in a brand-synergistic way. I find that splitting these into three separating commits makes life easier for my fellow coders when it comes time to review my pull request. The data-layer changes may include a database migration or unit tests, for example, and I prefer to keep those associated closely with the code changes that they correspond with, rather than modifications to views or JavaScript or whatnot.</p><h3>git add --patch</h3><p>So now I’ve got a bunch of local changes, but rather than lump them all into one commit, I want to create several commits out of them all. I reach for git add --patch, which launches an interactive tool that looks at each local modification and asks you what to do with it.</p><h4>simple example</h4><p>Let’s start small: a plain-text TODO list. Here’s what we are starting with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8655faa2187aacff1f9434379cc3fd95/href">https://medium.com/media/8655faa2187aacff1f9434379cc3fd95/href</a></iframe><p>After a day of hard work and self-reflection, I’ve modified this file quite a bit but haven’t yet committed anything. If I do a git diff here are the changes I see:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9ec46353f1e71932dfa76b31bba57c2/href">https://medium.com/media/c9ec46353f1e71932dfa76b31bba57c2/href</a></iframe><p>So I’ve added a bunch of new lines to this file and slightly tweaked what <em>were</em> the first and last lines of the file. Note that the two lines in the middle — about commit messages and pull requests — are unchanged.</p><p>I could commit this whole diff and give it some generic message like “update TODOs.” However, that’s not very descriptive; instead I’d like to separate this into two commits, with one focusing on the git-related edits at the top of the file and the other focusing on the automation-related edits at the bottom. (Yes, this is a contrived and overly-simple example…)</p><p>Here we go! I run git add --patch and here’s what I am greeted with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f99c13630e2729e5d0801ad5ea5c396d/href">https://medium.com/media/f99c13630e2729e5d0801ad5ea5c396d/href</a></iframe><p>This output is very similar to a standard git diff output but with an important question at the bottom: Do I want to <strong>stage this hunk</strong>? Git is asking whether I want to add (“stage”) the diffs that it lists above (“this hunk”) to its internal list of changes-to-be-committed.</p><p>The responses available to me are listed in brackets there, but I always forget what they stand for, so I’ll respond with ? to see if it will explain:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb2c1a5973609a884e9d903cf9034bc6/href">https://medium.com/media/fb2c1a5973609a884e9d903cf9034bc6/href</a></iframe><p>Whew, even more possible responses! Answering y will add that whole diff, which isn’t quite what I want. Answering n won’t stage any of it, which isn’t what I want either. q will cancel this whole git add --patch, but I’m not done yet… Toward the bottom it says that answering s will “split the current hunk into smaller hunks.” That sounds right!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/745086b70eaf0e1a7e9edfe1d3ef0aa5/href">https://medium.com/media/745086b70eaf0e1a7e9edfe1d3ef0aa5/href</a></iframe><p>When I answer s, Git notes that it has taken the previous bigger hunk (diff) and “Split into 2 hunks.” It can do this splitting easily <strong>because of the unchanged line(s) of text separating the blocks of edited text</strong>.</p><p>Now that I’ve split that single big diff into two smaller diffs, git add --patch goes back to asking what I want to do with each of these smaller diffs. The question is the same—“Stage this hunk?”—but now I can decide what to do with each smaller diff individually.</p><p>I’d like to commit the automation-related edits first and later commit the git-related edits. The first diff I’m asked about is the git-related hunk, so I’ll answer n to leave it out. With that answer, git moves on and asks about the next hunk:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46926fbdb76701a1165082d11dac25fd/href">https://medium.com/media/46926fbdb76701a1165082d11dac25fd/href</a></iframe><p>Now I’ll answer y as these next changes are the ones I want to commit first. This is the last hunk to analyze, so once I’ve answered this last question, thegit add --patch is done and I’m dumped back to the normal command-line.</p><p>What happened to my diffs? Let’s do a git status to see:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34aeb9a79e6853dd613d87c3f247dc4d/href">https://medium.com/media/34aeb9a79e6853dd613d87c3f247dc4d/href</a></iframe><p>My TODO file has some changes that are marked to be committed (a.k.a. “staged”), as well as some changes that are <em>not</em> going to be committed (a.k.a., you guessed it, “unstaged”). Let’s verify that the staged/unstaged diffs are what we expect. A standard git diff will only show the <em>unstaged</em> changes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f5592d2862dd5ccac9acf7c3a12bbc4/href">https://medium.com/media/2f5592d2862dd5ccac9acf7c3a12bbc4/href</a></iframe><p>Using git diff --staged will show the changes that are already staged:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42bebfcb43381c642dc44eb31b45ded7/href">https://medium.com/media/42bebfcb43381c642dc44eb31b45ded7/href</a></iframe><p>Looks good! Now a git commit --verbose will create a commit using just those staged changes. I can write a focused commit message to go along with it, such as “Add some specific automation goals.”</p><p>That’s only part of the changes I made to the file; I still need to commit the other changes. Back to git add --patch:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c66b609935c3dec000b2187437004805/href">https://medium.com/media/c66b609935c3dec000b2187437004805/href</a></iframe><p>This is the hunk of edits that I want in my next commit, so I’ll answer y here; it’s also the last hunk of local modifications, so after answering y I’m back on the command line. Just like with the first commit, I now have some staged edits but no unstaged edits this time, and git status proves it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/040f64718f1ec1b60797f8266010dd6e/href">https://medium.com/media/040f64718f1ec1b60797f8266010dd6e/href</a></iframe><p>…and git diff --staged shows that it’s now going to commit only the edits at the top of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34c1e384918dd06cc9d227c4877a7cc8/href">https://medium.com/media/34c1e384918dd06cc9d227c4877a7cc8/href</a></iframe><p>Another git commit --verbose with a commit message like “Add some git topics to investigate” and I’m done.</p><p>Ta-da! We just took a file with unrelated chunks of modifications and created logically separate commits to reflect that each chunk is in fact unrelated!</p><h4>While we’re at it…</h4><p>…let’s talk about commit messages for a minute.</p><p>Have you ever been trying to figure out what some existing chunk of code does (or why it even exists) and used git blame or git log --patch (or GitHub’s “Blame” and “History” buttons) on a file in order to look into its past? How often do you end up finding a commit that tweaks the existing code you’re looking at, but the commit message is something like “code review feedback” or “refactor”? Or something like “implement feature A” along with hundreds of lines of edited code that don’t seem related to feature A? That’s not very helpful, is it?</p><p>So, here we are creating little self-contained commits… Let’s make sure we have helpful messages to go along with them! Even though “code review feedback” may <em>technically</em> describe what you’re doing, it really isn’t <em>helpful</em> for anyone who may look at this commit in the future. A code archaeologist in the future is going to be curious about <em>why</em> we are making changes to code, and “code review feedback” isn’t as explanatory as it could be: What was the feedback? If there was lots of feedback, what piece of feedback is being addressed?</p><p>I get lots of feedback on my pull requests (e.g. “this method name isn’t clear,” “indentation is off,” “use this helper function rather than reimplementing it,” etc). Rather than make one big commit that addresses all of it — with a generic message like “review feedback” — I’ll use our new friendgit add --patch to split apart each type of change I need to make and then each commit can have a nicely explanatory message.</p><p>For example, one commit can be “renaming method,” and its diff will only show that name change in every place that method is called in the code base. The next commit can be “fix indentation,” and the diff shows that there are only changes to whitespace and no changes to text. The last commit can be “use existing helper function,” and the diff shows that I removed my reimplementation and instead used the existing helpers. Ta-da—in the future when I want to figure out why a line of code is using the wrong helper function, I’ll reach for git blame and end up on a commit that only changed one thing, instead of having to wade through whitespace changes and method renames before I can identify where I swapped out my implementation for the helper function and <em>then</em> try to find where I fat-fingered something.</p><p>Here’s another tip for your commit messages, for which Future You will at some point end up thanking Present You: Associate <em>every</em> commit in your codebase with an issue/bug/feature tracker identifier. (If you’re not already using an issue/bug/feature tracker, start now; even GitHub’s built-in Issues is better than nothing.) It’s as easy as adding a final line to your commit message with just “#13” which GitHub will turn into a link to the pull request or issue with id=13. If you’re using something like Pivotal Tracker or JIRA, you could use the full URL of the story (or, if you think you’ll never forget what the abbreviations stand for, just a shortened identifier).</p><p>If you’re like me, however, you’ll quickly forget to do this each time you commit. Fortunately, Git provides <a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks">a ton of hooks around important events</a>, and we can use the prepare-commit-msg hook to control the default message template that the --verbose flag uses. <a href="https://github.com/alxndr/dotfiles/blob/95ffd22753172cc6875b1b1244b8352dd3cda036/git-hooks/prepare-commit-msg">Here is the shell script I use on </a><a href="https://github.com/alxndr/dotfiles/blob/95ffd22753172cc6875b1b1244b8352dd3cda036/git-hooks/prepare-commit-msg">prepare-commit-msg</a>, which extracts the ticket identifier from the name of the branch I’m working in and appends it to the default template. Use this template by <em>always</em> using the --verbose/-v flag on git commit!</p><h4>A trickier --patch example</h4><p>We’ve looked at how to take a file with many modifications and create separate commits out of them. This is easy to do when the modifications are physically separated from each other — git add --patch can pretty much do it all for us. But what if the modifications are jumbled together, like all the edits I’ll want to do after the first round of code review on a pull request of mine?</p><p>As an example, let’s start off with a file that solely contains this JavaScript function that sums up an array of numbers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f91f6b541e4dab3e726b0681ec9d5116/href">https://medium.com/media/f91f6b541e4dab3e726b0681ec9d5116/href</a></iframe><p>If I submitted that for code review, my helpful colleagues would point out that A) I haven’t given my variables very human-readable names; B) we’re supposed to be using ES2016 in this project so any use of var is right out; and C) I might want to look into using Array.prototype.reduce to avoid the explicit for loop.</p><p>So I begin my refactoring, and first I rename all those variables. Then I change those vars to lets and that function to a =&gt;. Then I look up the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce">docs for </a><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce">Array.prototype.reduce</a> and end up replacing the for loop with a one-liner. Now here’s what I’ve got:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a285dedc15cddb057f7343bf17da349f/href">https://medium.com/media/a285dedc15cddb057f7343bf17da349f/href</a></iframe><p>Going from the original file to this one-liner is too big a change to wrap up into a single commit, so I want to split it into several commits that reflect the gradual changes I made along the way. I could revert back to the original and then tweak-and-commit and tweak-and-commit… Instead, let’s see how git add --patch can do all that for us!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66e0c4e7ff51289c5902f3241cd36c80/href">https://medium.com/media/66e0c4e7ff51289c5902f3241cd36c80/href</a></iframe><p>Since these edits I’ve made cover the entire file, trying to split the edits with s won’t work — Git will just repeat the “Stage this hunk” question, because it can’t break this diff apart any further. If we look at the list of all options with ? again, there’s one that will do what we want: e will “manually edit the current hunk.” Answering with e pops up our favorite text editor with these contents:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63e3fd2ba4e9b9d9e14b95520fdfbac2/href">https://medium.com/media/63e3fd2ba4e9b9d9e14b95520fdfbac2/href</a></iframe><p>This looks like the diff we just saw, but it will become <strong>instructions for Git to decide what parts of the modified file to include in the new commit</strong>. The lines which start with a — (a hyphen) are in the original file and will be deleted; every line that starts with a space occurs in the original <em>and</em> the edited file (i.e. it’s untouched); and every line that starts with a + (a plus sign) will be added. (The lines starting with # are ignored.) Thus, if I saved this as is, I’d end up with one big commit that changes everything (not what I want).</p><p>To build some custom step-by-step instructions for Git, I start with the existing code. These instructions work on a line-by-line basis, so any line of existing code that I want to edit will be represented as a deletion of the existing line and then an addition of a “new” line with the modification. Any lines of code that won’t be touched are prefixed with a space to indicate they are left alone.</p><p>Let’s start small, with changing the variable names.</p><p>Every existing line of code that uses one of the variables I want to rename will need to be “removed” (by prepending a —) and then “re-added” right below it with whatever edit we want to do (by prepending a +):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ef895c820f7b1f11bd52a530f8fa317/href">https://medium.com/media/4ef895c820f7b1f11bd52a530f8fa317/href</a></iframe><p>Note that the two lines that only contain a curly-brace } aren’t being modified, so they are prepended with a space rather than a — or +.</p><p>Now I can save and quit my editor, and Git creates the commit based on those instructions. (If Git can’t follow the instructions, you’ll see a message saying something like “could not apply cleanly.” This can be caused by having a deletion line where the code isn’t exactly what is already in the file; Git won’t be able to tell what your intention is.)</p><p>git diff --staged shows what this commit will change:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdc7b7202cd1a488f3df1c9b49724015/href">https://medium.com/media/bdc7b7202cd1a488f3df1c9b49724015/href</a></iframe><p>(That diff looks pretty similar to the instructions that I just created, doesn’t it? The removals and additions have been grouped together, but the curly brace lines still start with extra spaces and everything.)</p><p>I’ll commit this with an explanatory message — something like “change variable names.”</p><p>That takes care of one piece of feedback from my code reviews. The next one is that this should be in Fancy New JavaScript, using let or const instead of var and arrow functions rather than the function keyword. I fire up git add --patch and answer e again to edit the next commit hunk:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ff14f9167f76c70789db33cdd888c25/href">https://medium.com/media/1ff14f9167f76c70789db33cdd888c25/href</a></iframe><p>(Note that now the existing code is the version with the nicely named variables. That’s one step closer to the final result!)</p><p>Here again I’m going to use the existing/old code to create the intermediate/new code. The lines that need to be modified are repeated, with the old version starting with a — and the new version starting with a +; the lines that can stay the same have their initial -s changed to spaces:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af1689285747ac1ea2fbdb5050ad5744/href">https://medium.com/media/af1689285747ac1ea2fbdb5050ad5744/href</a></iframe><p>When I save this next set of commit instructions, Git readies the new commit. I can verify how it will look with git diff --staged:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1457a853e70ac14cd67196d1c482a8c/href">https://medium.com/media/a1457a853e70ac14cd67196d1c482a8c/href</a></iframe><p>Now I’ll give that commit a nice message like “tweaks for ES2015,” and then it’s on to the next one! The last bit of feedback I received was to look at Array.prototype.reduce rather than the for loop. git add --patch then e again:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef76dad256fcedb10d6cded77f36c697/href">https://medium.com/media/ef76dad256fcedb10d6cded77f36c697/href</a></iframe><p>I’m getting close! This can still be broken into some smaller steps; let’s start by replacing only the for loop:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/888042187b4cb2da37b1ee02b1456729/href">https://medium.com/media/888042187b4cb2da37b1ee02b1456729/href</a></iframe><p>I save and close; Git readies the commit; I give it a message like “replacing loop with reduce.” Back to git add --patch, and again I answer e:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ccc45203e8396b6e40a834df0fb6d28/href">https://medium.com/media/8ccc45203e8396b6e40a834df0fb6d28/href</a></iframe><p>Getting closer… I’ll refactor out that result variable now:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a489a05290910ecd62a1cdd189d02c3f/href">https://medium.com/media/a489a05290910ecd62a1cdd189d02c3f/href</a></iframe><p>Then I commit that with a message like “refactor out unnecessary variable.” Now those arrow functions can collapse into one-liners! Back to git add --patch, e, and here’s the hunk I’ll end up with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9457d11ac736ebac956530197e94aec0/href">https://medium.com/media/9457d11ac736ebac956530197e94aec0/href</a></iframe><p>After I commit that with a message like “one-line functions,” a standard git diff will show the difference between that last commit and the final post-review version of the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8a4f416336915a75cf7889d7100473d/href">https://medium.com/media/f8a4f416336915a75cf7889d7100473d/href</a></iframe><p>Now that’s reasonable enough to be its own commit, no --patch needed. It gets a commit message as well, and then ta-da, I’m done.</p><p>Check out how pretty the git history is!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdd79f77f86c1d432ed87810c1ed4abb/href">https://medium.com/media/bdd79f77f86c1d432ed87810c1ed4abb/href</a></iframe><p>Isn’t that a lot better than “initial sum.js” and then “code review changes”?</p><p>Whew, that’s it for now. <a href="http://medium.com/p/e986d4a64c21">Tune in next time for a look at </a><a href="http://medium.com/p/e986d4a64c21">git commit --amend and </a><a href="http://medium.com/p/e986d4a64c21">git rebase --interactive.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0966a562b10" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bleacher-report-engineering/small-commits-for-fun-and-profit-part-1-git-add-patch-c0966a562b10">Small Commits for Fun and Profit, part 1: git add --patch</a> was originally published in <a href="https://medium.com/bleacher-report-engineering">Bleacher Report Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>