The letter A styled as Alchemists logo. lchemists Syndication Icon

Putin's War on Ukraine - Watch President Zelenskyy's speech and help Ukraine fight against the senseless cruelty of a dictator!

Published May 30, 2020 Updated May 30, 2020

Git Commit Fixup

Demonstrates crafting a commit for fixing an existing commit.


# Hello and welcome to the Alchemists Screencasts!
# Today, we'll learn about Git Commit Fixup.

# Before starting, I strongly recommend watching these screencasts:
# - Git Rebase Abbreviations
# - Git Rebase Fixup
# The above will provide context to techniques used in this screencast.

# OK, let's begin.
# For context, we have a Ruby script that adds two numbers:

ruby calc.rb 1 2

# There's a problem, though.
# Our team requested we print mathematical calculations instead of sentences.
# No problem, let's fix the original implementation:

vi calc.rb

ruby calc.rb 1 2

# At this point, you might be tempted to save these changes as a separate commit.
# The problem is that this change is a *fix* to the original commit.
# We definitely don't need to historically preserve a separate commit for this.
# To solve this problem, we can make a fixup commit:


# 💡 See *Git Log Pretty* screencast for `gl` alias details.

git add calc.rb

git commit --fixup

# Notice our last commit is prefixed `fixup!`.
# This commit shares the same subject as the original commit:

printf "%s\n" "Added calculator implementation"

# The "fixup!" prefix is a special directive.
# This informs Git Rebase to associate our "fixup!" with the original commit. 🎉
# We can use Git Interactive Rebase to illustrate:

git rebase --interactive

# I aborted the interactive rebase to:
# 1. Show what rebase would do had we not inspected via interactive rebase.
# 2. Illustrate the original and fixup commits were grouped together.

# Here's our project history, again:


# With the above understood, we can rebase our changes:

GIT_EDITOR=true git rebase --interactive

# Notice we rebased *without* using the editor *and* our fix was applied. 🎉
# Use of `GIT_EDITOR=true` is important.
# This tricks Git into thinking the editor saved without error.
# ...and saves us a step since we don't need to confirm the fixup.
# ⚠️ This is only possible if you have AutoSquash support enabled.
# Even better, our implementation doesn't exhibit the original bug:

ruby calc.rb 1 2

# Here's what I love about this workflow:
# 1. We avoided the Git Rebase TODO Editor because user interaction is unnecessary.
# 2. We avoided editing the commit message because it was only an implementation fix.
# 3. Our implementation looks as if it was crafted perfectly the first time. 🎉
# 4. Future readers will have a high signal to noise.

# Enjoy!
# ☿ 🜔 🜍 🜂 🜃 🜁 🜄