The letter A styled as Alchemists logo. lchemists
Published October 29, 2019 Updated February 16, 2020

Git Rebase AutoSquash

Demonstrates how to configure Git to automatically fixup, squash, and amend commits.

Transcript

# Hello and welcome to the Alchemists Screencasts!
# Today, we are going to look at configuring Git Rebase AutoSquash support.
# Before we start, let's understand the problem by looking at our project structure:

tree

# ...and our Git History:

gl

# ...and what the Ruby script in this project does:

ruby calc.rb 1 2

# Hrm...well, that's a bug. Let's fix it!

vi calc.rb

ruby calc.rb 1 2

# That's better as the script properly handles simple arithmetic now.
# Since the original implementation introduced the bug, let's fix the associated commit:

git status --short --branch
ga calc.rb
gl
git commit --fixup <sha>
gl

# With our fixup commit in hand, we can rebase so Git will resolve our fixup commit.

git rebase --interactive

# Well, that was a lot of work (i.e. copy, pasting, and moving lines around).
# Luckily, Git has a solution for this and it's called: AutoSquash.
# Here's how to enable it:

git config --add rebase.autoSquash true

# OK, let's rebase again and see what happens:

git rebase --interactive

# By enabling Git Rebase AutoSquash support, Git automatically completed two tasks for us:
#   1. Our `fixup!` commit was *moved* next to the original commit to be *fixed up*.
#   2. The `fixup!` commit was renamed from a `p` (i.e. pick) to a `f` (i.e. fixup)`.
# That's a significant speed boost to our workflow. 🎉
# Let's review again (this time we'll apply the changes too):

git rebase --interactive

gl

# Now we have a clean Git History.
# Even better, the original implementation bug was erased so none is the wiser. 😉
# 💡 This technique works when squashing too (i.e. `git commit --squash <sha>`).
#
# I'd recommend adding this to your global configuration so all projects benefit.
# Example:
#   `git config --global --add rebase.autoSquash true`
#
# As engineers, it's important to let our tools automate as much for us as possible.
# This is a step in achieving that goal. 🎉
#
# Enjoy!
# https://alchemists.io
# ☿ 🜔 🜍 🜂 🜃 🜁 🜄