<?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: Maxime Bréhin</title>
    <description>The latest articles on Forem by Maxime Bréhin (@mbrehin).</description>
    <link>https://forem.com/mbrehin</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%2F926826%2Fbc9da984-dfdd-48e2-b3a2-9a3b0984d8b5.jpeg</url>
      <title>Forem: Maxime Bréhin</title>
      <link>https://forem.com/mbrehin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mbrehin"/>
    <language>en</language>
    <item>
      <title>Git protip: quickly rewrite an old commit message</title>
      <dc:creator>Maxime Bréhin</dc:creator>
      <pubDate>Mon, 19 Sep 2022 09:43:00 +0000</pubDate>
      <link>https://forem.com/mbrehin/git-protip-quickly-rewrite-an-old-commit-message-20h3</link>
      <guid>https://forem.com/mbrehin/git-protip-quickly-rewrite-an-old-commit-message-20h3</guid>
      <description>&lt;p&gt;Just as I sometimes &lt;a href="https://dev.to/en/articles/git-protip-autofixup/"&gt;forget files in a commit&lt;/a&gt;, &lt;strong&gt;I also sometimes botch commit messages&lt;/strong&gt; only to find out about it a while later (after a few more commits). My most common oversight is probably forgetting to &lt;a href="https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword"&gt;link the issue&lt;/a&gt; the commit refers to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Later, but not too late!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Don't tell me you're thinking: *“Never mind, I'll pop open the UI and fill it in by hand!” or that you're giving up! Maybe that's because you don't know about &lt;strong&gt;interactive rebasing&lt;/strong&gt;, or are afraid to use it 😨. Honestly, you shouldn't be, with &lt;a href="https://dev.to/en/trainings/360-git/"&gt;a little learning&lt;/a&gt; this command proves to be a super valuable ally!&lt;/p&gt;

&lt;p&gt;Apart from the rebase thing, do you know that you can create a commit that expresses the intent to change the message? I bet you don't! Fortunately, I'm here 😁 to help. Let me introduce the &lt;code&gt;git commit --fixup reword:&amp;lt;commit-ref-to-fix&amp;gt;&lt;/code&gt; command. This is a bit of a mouthful but who cares, we're going to wrap it with a nice alias that looks kind of like a magic incantation 🔮. Introducing &lt;strong&gt;autoreword&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; alias.autoreword &lt;span class="s1"&gt;'!git commit --fixup reword:$1 &amp;amp;&amp;amp; git rebase --autosquash --interactive --rebase-merges $1~1 &amp;amp;&amp;amp; echo "autoreword finished"'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens if you run this command?&lt;/p&gt;

&lt;p&gt;&lt;del&gt;🌀 An evil wormhole opens, releasing the flames of hell that reduce this world to ashes 🔥!&lt;/del&gt; &lt;em&gt;(Actually no, we don’t even need to use magic for that 😭)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But seriously, Git will open your editor with a message starting with &lt;code&gt;amend! …&lt;/code&gt; followed by the first line of the commit message you want to fix, then a line break and again the (full) commit message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;amend! chore&lt;span class="o"&gt;(&lt;/span&gt;dx&lt;span class="o"&gt;)&lt;/span&gt;: setup ESLint and Prettier

chore&lt;span class="o"&gt;(&lt;/span&gt;dx&lt;span class="o"&gt;)&lt;/span&gt;: setup ESLint and Prettier

&lt;span class="c"&gt;# Please enter the commit message for your changes. Lines starting&lt;/span&gt;
&lt;span class="c"&gt;# with '#' will be ignored, and an empty message aborts the commit.&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# On branch chore/automation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then change the message (but keep the very first line as-is).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;amend! chore&lt;span class="o"&gt;(&lt;/span&gt;dx&lt;span class="o"&gt;)&lt;/span&gt;: setup ESLint and Prettier

chore&lt;span class="o"&gt;(&lt;/span&gt;dx&lt;span class="o"&gt;)&lt;/span&gt;: setup ESLint and Prettier

&lt;span class="c"&gt;# Here is the reference to my issue!&lt;/span&gt;
Closes &lt;span class="c"&gt;#42&lt;/span&gt;

&lt;span class="c"&gt;# Please enter the commit message for your changes. Lines starting&lt;/span&gt;
&lt;span class="c"&gt;# with '#' will be ignored, and an empty message aborts the commit.&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# On branch chore/automation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can save and close the file.&lt;/p&gt;

&lt;p&gt;Git will then create the &lt;em&gt;fixup&lt;/em&gt; commit and &lt;strong&gt;our alias will automatically run the interactive rebase&lt;/strong&gt;, starting from the faulty commit’s direct ancestor. It opens your editor one last time with the list of actions tu run so you can see that &lt;strong&gt;the fixup commit is already at the right spot in the list&lt;/strong&gt; prefixed by &lt;code&gt;fixup -C&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pick 3f0714b chore&lt;span class="o"&gt;(&lt;/span&gt;dx&lt;span class="o"&gt;)&lt;/span&gt;: setup ESLint and Prettier
fixup &lt;span class="nt"&gt;-C&lt;/span&gt; 61eecda amend! chore&lt;span class="o"&gt;(&lt;/span&gt;dx&lt;span class="o"&gt;)&lt;/span&gt;: setup ESLint and Prettier
pick 9a7cf39 chore&lt;span class="o"&gt;(&lt;/span&gt;dx&lt;span class="o"&gt;)&lt;/span&gt;: setup Husky, lint-staged, precommit-checks, commitlint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ve got nothing more to do but verify, save and close the file. The rebase then runs (without conflict) and &lt;strong&gt;you can verify your log to see that the commit message was updated!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You may see in the actions list some keys like &lt;code&gt;label onto&lt;/code&gt; and &lt;code&gt;reset onto&lt;/code&gt;. This is a because the rebase is called with a &lt;code&gt;--rebase-merges&lt;/code&gt; option that preserves your local merges while rebasing. You don’t need to worry about it 😌.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>protip</category>
      <category>commit</category>
    </item>
    <item>
      <title>Git "autofixup"</title>
      <dc:creator>Maxime Bréhin</dc:creator>
      <pubDate>Thu, 15 Sep 2022 13:54:30 +0000</pubDate>
      <link>https://forem.com/mbrehin/git-autofixup-5b3g</link>
      <guid>https://forem.com/mbrehin/git-autofixup-5b3g</guid>
      <description>&lt;p&gt;How many times did you &lt;strong&gt;forget to add a file or change&lt;/strong&gt; to a commit, only to discover your mistake later on (after a few commits)?&lt;/p&gt;

&lt;p&gt;Since I'm a strong proponent that &lt;strong&gt;atomic commits&lt;/strong&gt; are key to &lt;strong&gt;project quality and automation&lt;/strong&gt; (see &lt;a href="https://www.conventionalcommits.org/"&gt;Conventional Commit&lt;/a&gt; and &lt;a href="https://semantic-release.gitbook.io/"&gt;Semantic Release&lt;/a&gt;, for instance), I do my best to fix these commits using Git &lt;strong&gt;interactive rebasing&lt;/strong&gt;. I strongly recommend you do the same. You don’t have to be afraid of Git rebase, you just have to &lt;a href="https://delicious-insights.com/en/trainings/360-git/"&gt;learn how to use it&lt;/a&gt; 😉.&lt;/p&gt;

&lt;p&gt;My goal here is to help you do this in a quick, smooth, painless way.&lt;/p&gt;

&lt;p&gt;You may already be familiar with the &lt;code&gt;git commit --fixup&lt;/code&gt; option (or &lt;code&gt;--squash&lt;/code&gt;) that creates a commit with a special-format message about our intent to fix another commit. Using that, when you run a properly-configured &lt;strong&gt;interactive rebase&lt;/strong&gt;, Git will automatically move that commit line as a &lt;em&gt;fixup&lt;/em&gt; next to the one you're fixing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by step
&lt;/h2&gt;

&lt;p&gt;Add the missing files to the stage: &lt;code&gt;git add …&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Ask Git to create the fixup commit: &lt;code&gt;git commit --fixup=&amp;lt;commit-ref-to-be-fixed&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then run an interactive rebase, starting one commit before the one you fixed: &lt;code&gt;git rebase --autosquash -i -r &amp;lt;fixed-commit-ref&amp;gt;~1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Git opens your editor with the list of actions to run, but with the &lt;em&gt;fixup&lt;/em&gt; already in the right spot. &lt;strong&gt;There's nothing more to do except save and close the file&lt;/strong&gt;, that’s it !&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pick 3f0714b chore&lt;span class="o"&gt;(&lt;/span&gt;dx&lt;span class="o"&gt;)&lt;/span&gt;: setup ESLint and Prettier
fixup 61eecda fixup! chore&lt;span class="o"&gt;(&lt;/span&gt;dx&lt;span class="o"&gt;)&lt;/span&gt;: setup ESLint and Prettier
pick 9a7cf39 chore&lt;span class="o"&gt;(&lt;/span&gt;dx&lt;span class="o"&gt;)&lt;/span&gt;: setup Husky, lint-staged, precommit-checks, commitlint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might want to check your log to confirm everything’s fine. You may also want to check that all the intended files were recorded by the fixed commit &lt;code&gt;git show --name-only &amp;lt;new-commit-ref&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As this is a recurring thing for me, I made a "magic" 🧙‍♂️ alias that calls both commands in sequence (to fix a single commit):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Don't forget to stage the relevant fixes first&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; alias.autofixup &lt;span class="s1"&gt;'!!GIT_EDITOR=true &amp;amp;&amp;amp; git commit --fixup $1 &amp;amp;&amp;amp; git rebase --autosquash --interactive --rebase-merges $1~1 &amp;amp;&amp;amp; echo "autofixup finished"'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Et voilà !&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(The Git config you may love: there are many useful aliases you can add to your Git configuration. You can &lt;a href="https://gist.github.com/tdd/470582"&gt;check out the one we’ve built through years of experience&lt;/a&gt; to learn more.)&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>codequality</category>
      <category>alias</category>
    </item>
  </channel>
</rss>
