Git Rebase Squash
Demonstrates how to use rebase to squash a previously created commit.
# Hello and welcome to the Alchemists Screencasts! # Today, we'll learn about Git Rebase Squash. # We'll do this by working on a Ruby script that adds two numbers: ruby calc.rb 1 2 # 💡 If you've watched the *Git Rebase Fixup* screencast, this'll look familiar. # Our team has requested we print mathematical calculations instead of sentences. # We can do this by fixing the original implementation: vi calc.rb
ruby calc.rb 1 2 # Much better, let's commit these changes: git commit --all
# 💡 `--all` was used to pick up all changes (in this case a single file). # To continue, let's study the Git log: gl # 💡 To learn more about the `gl` alias, see the *Git Log Pretty* screencast. # Notice the third commit is where the original implementation was added. # For clarity, here's the subject used: "Added calculator implementation". # The plan is to *squash* our last into the above above commit using Git Rebase. # Here's a review of the commit to be squashed:
git show # Before we start, pay attention to how I move and change the commits. # We'll discuss more after the rebase is complete: git rebase --interactive
# If you didn't catch all of that, feel free to rewind and watch again. 😉 # Let me explain, though: # 1. The squash commit was moved to the commit that needed squashing. # 2. "p" (pick) was changed to "s" (squash) so Git knows to *squash* the commit. # 3. We saved and exited to let Git perform our instructions. # 4. Git Rebase halted, during the squash, so we could edit the commit *body*. # The difference between squash and fixup is that squash edits the commit. # The best part of squashing is to combine multiple thoughts into one. # This is why squashing without editing or worse, merge squashing, is harmful. # 💡 A well written, thought out Git history, is a valuable learning/debugging asset. # OK, let's return to the Git log: gl # Notice how the squashed commit is gone! # Even better, the original implementation is fixed *and* commit message updated:
git show # We can test this further by running the implementation: ruby calc.rb 1 2 # Here's what I love about this workflow: # 1. We fixed the original implementation while erasing our mistake. # 2. We edited the original commit message to tell a complete story. 🎉 # 3. Reviewers will have a high signal to noise when providing feedback. # 4. Our implementation looks as if it was crafted perfectly the first time. 🎉 # By the way, you can use `git commit --squash` to speed up this process further. # (we'll cover that in a future screencast, though) # Enjoy! # https://www.alchemists.io # ☿ 🜔 🜍 🜂 🜃 🜁 🜄