Git Commit Amend
Demonstrates how to quickly modify the last commit you made.
# Hello and welcome to the Alchemists Screencasts! # Today, we'll learn about Git Commit Amend. # Here's our project structure and Git log: tree gl # 💡 See *Git Log Pretty* screencast for `gl` alias details. # As you can see, this is a simple Ruby project with a single script: ruby calc.rb 1 2 # Hey, wait, our math is all wrong!?! 😅 # Let's fix the code: vi calc.rb
ruby calc.rb 1 2 # Much better! # Since our last commit introduced the bug, we can amend to fix it: git add calc.rb git commit --amend
gl # The ability to amend a commit *only* works on the *last* commit. # Amending a commit is the fastest way to apply corrections to the previous commit. # The bug is not only fixed but the implementation appears correct from the start. 🎉 # You might have noticed, when amending, we have the ability to edit the commit message. # What about situations in which you only need to amend the implementation? # Well, we can speed that up too. # How about we print mathematical calculations instead of sentences: vi calc.rb
# Now we can amend without editing the commit message: git add calc.rb git commit --amend --no-edit gl ruby calc.rb 1 2 # That was definitely faster. # Notice that we've been using `git add`, though. # We can skip that step too. # This time we'll modify the script to do multiplication: vi calc.rb
ruby calc.rb 2 3 # Now we can amend these changes via a single command: git commit --amend --all --no-edit git status --short --branch gl # Much faster! # Note that `--all` will pick up staged changes too so be careful you don't grab too much. # For this situation, `--all` is perfect. # We need to make one last correction due to the commit message being outdated: git commit --amend
git show # We could have corrected the body earlier but wanted to demonstrate `--all`. # At least now you can see the many uses of amending commits. # Enjoy! # https://alchemists.io # ☿ 🜔 🜍 🜂 🜃 🜁 🜄