<?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 Jacek Smolak on Medium]]></title>
        <description><![CDATA[Stories by Jacek Smolak on Medium]]></description>
        <link>https://medium.com/@jacek.smolak?source=rss-f6b265b5f37f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*WGrZpFr8APHCKHJhBEXwiA.jpeg</url>
            <title>Stories by Jacek Smolak on Medium</title>
            <link>https://medium.com/@jacek.smolak?source=rss-f6b265b5f37f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 14:18:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jacek.smolak/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[How do I commit]]></title>
            <link>https://medium.com/@jacek.smolak/how-do-i-commit-59bf6de2b264?source=rss-f6b265b5f37f------2</link>
            <guid isPermaLink="false">https://medium.com/p/59bf6de2b264</guid>
            <category><![CDATA[good-practices]]></category>
            <category><![CDATA[code-review]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[version-control]]></category>
            <dc:creator><![CDATA[Jacek Smolak]]></dc:creator>
            <pubDate>Mon, 02 Jan 2017 19:35:02 GMT</pubDate>
            <atom:updated>2017-01-02T19:35:02.884Z</atom:updated>
            <content:encoded><![CDATA[<p>Over past several months I have developed a certain way of commiting my files. One that leaves my code in a very good and organized shape, one that makes the flow of data in application being readable like a good book.</p><p>In this article I will also tell you how I do it. I will also show you few git commands, which I use, to achieve this.</p><p>But first …</p><h3>Have you ever seen one of these?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*r9lChZT5_EzVft6BHhjVsw.png" /><figcaption>One commit, many files</figcaption></figure><p>… combined with</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/131/1*Jb4MVzgSxzW5V-8CzQ7hYw.png" /><figcaption>Lot of added code</figcaption></figure><p>… including those</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*HpDYli-kO-SMg2FMCHEXWQ.png" /><figcaption>Artificial commits</figcaption></figure><p>I bet you did. I will even bet you (including myself) have done something like that in your career.</p><p>But before I go any further, let me also introduce a bad character in this story. His name is:</p><h4>Artificial commit</h4><ul><li>one that has no meaning (whatsoever) to commits history</li><li>one which content can’t be found by commit message</li></ul><p>Now, having introduced all the wrong ways of commiting things, let’s see how we can fix them.</p><h3>Every application has some kind of a data flow</h3><p>For instance, if I would like to show you a bunch of pictures, in a Redux-like application, I would need:</p><ol><li>A picture component</li><li>Gallery component</li><li>Page component</li><li>State data to pass to props</li><li>Container component</li><li>Reducer to populate state</li><li>Action to trigger reducer</li><li>REST-like client to fetch data from some API</li><li>Action on page enter</li><li>Router with routes</li></ol><p>(oh, and don’t worry if you won’t understand what’s going on up there, it’s just a very brief description of a flow of data in a product I am working on on a daily basis)</p><p>Now, how about having commits like that, but in <strong>reverse order</strong>?</p><blockquote>Why? — you asked</blockquote><p>I think you already know, why. But I will get there, hold on.</p><p>So, commits list (not their messages, but what they introduce):</p><ol><li>Router with routes</li><li>Action on page enter</li><li>REST-like client to fetch data from some API</li><li>Action to trigger reducer</li><li>Reducer to populate state</li><li>Container component</li><li>State data being passed to props</li><li>Page component</li><li>Gallery component</li><li>Picture component</li></ol><p>So, again, why this order?</p><h4>To provide dependencies for following commit(s).</h4><p>— this is THE most important part of this article (I wish I could color it).</p><p>Why, in my opinion, this approach is good? There are few reasons.</p><h4>1. It’s easier to do Code Review</h4><ul><li>it’s like reading a book, chapter after chapter</li><li>you won’t find any classes / functions / constants / etc. used before they were introduced (no more “<em>where the heck did that come from ... oh .. it&#39;s there, two commits later”</em>)</li><li>code is split into understandable chunks; even if PR is big, it will still be easy to grasp</li><li>in most cases there will be two files per commit: test + class</li></ul><h4>2. You can checkout to any given commit</h4><ul><li>application will work, always, up to given commit — all dependencies are there, remember?<br>(not to mention, all unit tests will have passed as well)</li></ul><h4>3. It’s really easy to apply changes</h4><ul><li>typo in one of the files? No problem, just change it in given commit and amend it (some real-life examples are described later on)</li><li>thus less chance of having artificial commits like Apply CR changes and stuff in master branch</li></ul><h4>4. One learns how the application works</h4><ul><li>having everything commited in sequence, produces, unintentionally, a great set of examples for newcomers (you don’t have to see all of the code there is to get to know how given feature work, just view it commit after commit, it’s that simple)</li><li>entry threshold is lower</li><li>want to discuss architectural approach for given PR? No problem, just review it, step by step and explain at which point you would take different path</li></ul><h4>5. Clean history</h4><ul><li>for millenia to come … or for at least few years till you’ll switch to new technology :)</li></ul><h3>Some real-life examples</h3><blockquote>Jacek, but I am used to commiting like every three minutes. What do I do then?</blockquote><p>You have a bunch of commits with changes, reverts, modifications, WIPs, etc. You would like to create a pull request but you know no one will read this mess.</p><p>No problem. Use git reset --mixed &lt;commit&gt; <em>(</em><a href="https://git-scm.com/docs/git-reset#git-reset---mixed"><em>docs</em></a><em>)</em></p><p>This will get all the files (with latest changes, up to given commit, but not included) untracked, so you can add and commit them in proper order and with meaningful messages later on.</p><p>History with strange messages:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*z05VIoHfQx68RupJIPPaPg.png" /><figcaption>What a mess …</figcaption></figure><p>Mix it with: git reset --mixed 11a7fa3 and you will have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*ECDPvB-Fa-UmuM33R78Syg.png" /><figcaption>Untracked files</figcaption></figure><p>Commit everything as it was supposed to be in the first place:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/1*zEdcwyXoLgzSFq66y1biCg.png" /><figcaption>Now that looks better, doesn’t it?</figcaption></figure><p>And yes, you could also do some squashing, but I find this easier most of the times.</p><blockquote>OK, reset mixed-style done. But Jacek, I need to have two commits (or more) for one file!</blockquote><p>Again, no problem.</p><p>Use git add -p <em>(</em><a href="https://git-scm.com/docs/git-add#git-add--p"><em>docs</em></a><em>)</em></p><p>This will allow you to select only those changes you are interested in and commit them. Other changes in that file will still be left to commit.</p><p>Let’s say you have introduced two changes in file3.js.<br>Type git add -p file3.js and you will see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*z256yPC8MgFk1WhiHxesDw.png" /><figcaption>Few changes in one file</figcaption></figure><p>In order to add only first part we need to split those changes, hit s:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*qi05p-fxB0DbB_HmsR0q5Q.png" /><figcaption>Changes are split</figcaption></figure><p>You will see that your changes were split into two chunks. Right now you are being asked what to do with first one. Let’s take it. Hit y and for the second part hit n:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*Dcjf6rbRdxmth6P4b0c-ng.png" /><figcaption>Take first part, leave the rest</figcaption></figure><p>Your current status will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*X4Qe52VBWx4GTPtaZBzpuA.png" /><figcaption>Partially added file</figcaption></figure><p>Changes to be commited can be seen by typing git diff --cached <em>(</em><a href="https://git-scm.com/docs/git-diff#git-diff-emgitdiffem--options--cachedltcommitgt--ltpathgt82308203"><em>docs</em></a><em>)</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/237/1*DiVShvxz0eYeW1h7VmBrJQ.png" /><figcaption>Cached changes, ready to be commited</figcaption></figure><blockquote>Can I create PR now?</blockquote><p>Yes.</p><p><em>(… code review process …)</em></p><p>Yet, afterwards, you learn that you need to fix some typos in file1.js and file2.js.</p><blockquote>How can I do it without leaving any artificial commits?</blockquote><p>First, commit those changes like you normally would:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*fQSn5gxIFbnuORMDedkmvA.png" /><figcaption>CR fixes</figcaption></figure><p>After code review, I would say: <em>LGTM</em> (which stands for Looks Good To Me — it’s how we tell our colleagues that their code requires no further changes and they are ready to merge).</p><p>Can you merge now? Well …I would say not yet. Code looks good, but commits can still be improved.</p><p>Imagine for a moment, that you would actually add something there (not fix typos), like a missing test case or better variable name. Normally one would say, that those commits are fine, because they introduce something new and thus can be left like that.</p><p>I would still disagree.</p><p>In terms of given context (that particular branch / task the PR is about) one could, and should, apply those changes in commits where the base code was introduced in the first place. So in our case:</p><ul><li>changes in 6307aa3 Fix typo in file1.jsshould be included in commit 7225ed6 Add file1.js</li><li>changes in b4ac8ba Fix typo in file2.js should be included in commit 7225ed6 Add file1.js</li></ul><p>The easiest way to clean this up is to:</p><ul><li>rebase interactively using git rebase -i HEAD~5 (you will do rebasing on last five commits; <a href="https://git-scm.com/docs/git-rebase#git-rebase--i"><em>docs</em></a>):</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/244/1*GtlOp-DC0eYJ8AfKyJ7iQw.png" /></figure><ul><li>reorder commits by changing lines order:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*lfZlwROlc7cqJCkMOS9W8A.png" /><figcaption>Change order of commits</figcaption></figure><ul><li>order squashing:<br><em>(hint: you always squash given commit to previous one)</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*PfGSidMco4SA5lpRhQyAvw.png" /><figcaption>s — for squashing</figcaption></figure><ul><li>commence rebase and once you see this:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*ITi_muIzRjEZr84FHsQGZA.png" /><figcaption>Now you decide which commit message to leave</figcaption></figure><ul><li>change it to this and continue:<br><em>(hint: you can either comment given line or remove it)</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*wAg6quuC2NtDuHiCNQj39A.png" /><figcaption>First commit message left, second commented</figcaption></figure><ul><li>repeat it with second one:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*elftO-Guy22eA3xkpX1Jzw.png" /><figcaption>Again, you pick the one you want</figcaption></figure><ul><li>in order to have this:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*3YHmqdqjKX024YYCV9YAWQ.png" /><figcaption>Second commit message commented</figcaption></figure><p>After you’ve done squashing, you will be left with only three commits:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*4IL5sMiVaIGdeYow-1_onw.png" /><figcaption>Final commits list</figcaption></figure><p>And now, now you can merge.</p><h4>Is that it?</h4><p>Yes. That is (almost) all you need to know.</p><p>Why <em>almost</em>? The things I’ve just written here cover most of the cases you will stumble across. I believe in your daily work you probably handle it in some other fashion. I am fine with that, I am fine with any approach. The final result is what matters.</p><p>I have not given an example of commits where you add files which are not represented by proper message (or vice versa). I hope you just don’t do it. And if you did, just use any combination of mentioned git methods to fix it.</p><p>Remember that one day one might search for something and expect it to be commented properly. Spare those few minutes of your time and improve what can (and should be) improved.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59bf6de2b264" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>