<?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>DEV Community: Bella Woo</title>
    <description>The latest articles on DEV Community by Bella Woo (@bellawoo).</description>
    <link>https://dev.to/bellawoo</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%2F331943%2F688f76a7-3316-40a0-847a-47a43c33989d.jpg</url>
      <title>DEV Community: Bella Woo</title>
      <link>https://dev.to/bellawoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bellawoo"/>
    <language>en</language>
    <item>
      <title>How to Fix a Typo After You've Already Pushed Your Commit</title>
      <dc:creator>Bella Woo</dc:creator>
      <pubDate>Fri, 07 Feb 2020 16:33:05 +0000</pubDate>
      <link>https://dev.to/bellawoo/how-to-fix-a-typo-after-you-ve-already-pushed-your-commit-342l</link>
      <guid>https://dev.to/bellawoo/how-to-fix-a-typo-after-you-ve-already-pushed-your-commit-342l</guid>
      <description>&lt;h3&gt;
  
  
  Or alternative title - How I learned to love Git rebase
&lt;/h3&gt;

&lt;p&gt;When I first started working with Git, I was indoctrinated into the school of merge and was told to never rebase. Rebasing lets you re-write history. The whole point of Git is to track history. Therefore, rebasing is bad.&lt;/p&gt;

&lt;p&gt;But there was one workflow that truly cemented my conversion and has become a regular part of my code writing process - rebasing on my active branch.&lt;/p&gt;

&lt;p&gt;Even with linters and a spell checker extension installed in my code editor, from time to time, I'll catch a typo I've committed to git. Or I'll forget a change in a lingering file. And because the basic push workflow has become muscle memory at this point, I would push the commit before I noticed the mistake. I would fix it and do one of these...&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ git commit -m "fix typo"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Gross.&lt;/p&gt;

&lt;p&gt;But we can fix this quickly with an interactive rebase!&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixup First
&lt;/h2&gt;

&lt;p&gt;After fixing my mistakes, I'll stage the file like normal. Then instead of &lt;code&gt;-m&lt;/code&gt; and the cringy message, I mark the commit with the &lt;code&gt;--fixup&lt;/code&gt; option. The command expects a commit SHA-1 to attach the fix to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git add .
$ git commit --fixup 710f0f8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Another neat trick is to refer to the previous commit as the parent of the current one. &lt;code&gt;HEAD~&lt;/code&gt;, &lt;code&gt;HEAD^&lt;/code&gt; will both work, as would &lt;code&gt;HEAD~2&lt;/code&gt;  to refer to the commit before last, or the grandparent of the current commit. Note that &lt;code&gt;~&lt;/code&gt; and &lt;code&gt;^&lt;/code&gt; &lt;a href="https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_ancestry_references"&gt;are not interchangable&lt;/a&gt;. Git is even smart enought to be able to find the first words of a commit message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# these will all fixup your commit to the previous one.
$ git commit HEAD~
$ git commit HEAD^
$ git commit :/update
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When I run &lt;code&gt;git log&lt;/code&gt;, the history will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f7f3f6d (HEAD) fixup! update variable name
310154e update variable name
a5f4a0d (master, origin/master, origin/HEAD) working code on master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Let's rebase!
&lt;/h2&gt;

&lt;p&gt;We add &lt;code&gt;-i&lt;/code&gt; to run the rebase in interactive mode and supply an argument of the parent of the last commit you want to edit. I use this as a rule: add 1 to the number of commits I need to go back. Adding &lt;code&gt;--autosquash&lt;/code&gt; will pick up any commits prefaced with &lt;code&gt;fixup!&lt;/code&gt; and set your interactive rebase session with the commands filled in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git rebase -i --autosquash HEAD~3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The result of that command will be a list of your commits in ascending order (my default opens in vim) along with the action git should apply when running the commit. Note that the last commit already has &lt;code&gt;fixup&lt;/code&gt; command attached.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pick a5f4a0d working code on master
pick 310154e update variable name
fixup f7f3f6d fixup! update variable name


# Rebase a5f4a0d..f7f3f6d onto a5f4a0d (3 commands)
#
# Commands:
# p, pick &amp;lt;commit&amp;gt; = use commit
# r, reword &amp;lt;commit&amp;gt; = use commit, but edit the commit message
# e, edit &amp;lt;commit&amp;gt; = use commit, but stop for amending
# s, squash &amp;lt;commit&amp;gt; = use commit, but meld into previous commit
# f, fixup &amp;lt;commit&amp;gt; = like "squash", but discard this commit's log message
# x, exec &amp;lt;command&amp;gt; = run command (the rest of the line) using shell
# d, drop &amp;lt;commit&amp;gt; = remove commit
# l, label &amp;lt;label&amp;gt; = label current HEAD with a name
# t, reset &amp;lt;label&amp;gt; = reset HEAD to a label
# m, merge [-C &amp;lt;commit&amp;gt; | -c &amp;lt;commit&amp;gt;] &amp;lt;label&amp;gt; [# &amp;lt;oneline&amp;gt;]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c &amp;lt;commit&amp;gt; to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
#       However, if you remove everything, the rebase will be aborted.
#
#
# Note that empty commits are commented out
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;At this point, if I'm doing a final clean up to push to a remote branch, I'll maybe &lt;code&gt;reword&lt;/code&gt; a commit message or maybe even &lt;code&gt;squash&lt;/code&gt; some extraneous commits together. It's worth having a read through the comments because this is a really powerful workflow, but &lt;code&gt;fixup&lt;/code&gt; is the one I use the most.&lt;/p&gt;

&lt;p&gt;Once you complete the rebase, your revised &lt;code&gt;git log&lt;/code&gt; should have a new singular commit that has combined your fixup commit with the previous one. &lt;em&gt;&lt;em&gt;bows and accepts applause&lt;/em&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2231360 update variable name
a5f4a0d (master, origin/master, origin/HEAD) working code on master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Autosquash magic
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;--autosquash&lt;/code&gt; will also pick up commits with &lt;code&gt;--squash&lt;/code&gt; option, but I tend to not want to keep that message, so fixup works just fine for me. Squash might be a good option if you have a significant amount of new code but want only one atomic commit.&lt;/p&gt;

&lt;p&gt;You can also set the following git config setting to omit having to include the autosquash option every time you run an interactive rebase.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ git config --global rebase.autosquash true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;My setups always have this set to true, which helped make fixups and squashing commits feel like second nature since to enter a rebase session, I only have to type &lt;code&gt;git rebase -i HEAD~3&lt;/code&gt; or however many commits I think I need to clean up.&lt;/p&gt;

&lt;h2&gt;
  
  
  And that's how I converted to rebase!
&lt;/h2&gt;

&lt;p&gt;Other helpful resources include this &lt;a href="https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History"&gt;git tutorial on revising history&lt;/a&gt;. Once I digested that, I found it easier to understand the &lt;a href="https://git-scm.com/docs/git-rebase"&gt;full doc on rebase&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>git</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
