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 March 30, 2020 Updated March 30, 2020

Git Rebase Edit

Demonstrates how to edit a previously created commit.


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

# Before starting, it might help to watch/rewatch these screencasts:
# - Git Rebase Fixup
# - Git Rebase Squash
# The above tackles a similar workflow.
# For context, here's the Ruby script we've been working on:

ruby calc.rb 1 2

# Our team has requested we print mathematical calculations instead of sentences.
# At first glance, it might seem like we can do the following:
# - Fix the original implementation.
# - Make a new commit explaining the changes.
# - Rebase to mend the original commit and clean up our history.
# 💡 Again, refer to the Git Rebase Fixup and Squash screencasts.
# Luckily, Git allows us to edit commits.
# Let's review the Git history:


# 💡 See the *Git Log Pretty* screencast to learn more about the `gl` alias.
# Here's the commit we need to edit:

git show

# There are a couple of issues with this commit:
# 1. Implementation needs fixing (as mentioned earlier).
# 2. Commit does not explain *why* it was made (i.e. the commit body).
# To correct, we'll use Git Rebase Edit:

git rebase --interactive

# If you didn't catch all of that, feel free to rewind and watch again. 😉
# Let me explain, though:
# 1. "p" (pick) was changed to "e" (edit) so Git knows to *edit* the commit.
# 2. We saved and exited to let Git perform the instruction.
# 3. Git stopped the rebase so we can edit our commit accordingly.

# We are stopped at our "Added calculator implementation" commit.
# Git instructs us next steps:
# 1. Use `git commit --amend` to edit the commit.
# 2. Use `git rebase --continue` to resume and finish the rebase.
# 💡 You can also use `git rebase --abort` to exit the rebase and start over.
# Lastly, if still not convinced you are editing the right commit, use:

git rebase --show-current-patch

# The above works like `git show` but sensitive to where you are in the rebase. 🎉
# 💡 Show current patch is so handy, you might want to alias it.
# Now we can fix our implementation:

vi calc.rb

ruby calc.rb 1 2

# Great, let's amend these changes as instructed by Git:

git commit --all --amend

# 💡 `--all` was used to avoid having to type `git add calc.rb`.
# Let's check our status:

git status --short

# Great, our changes are ammended properly.
# For more verbosity, use:

git status

# Once again, Git informs us of our next steps and its next steps. 🎉
# Did you notice we can edit the remaining Rebase TODOs?
# This is a handy feature and worth seeing before continuing the rebase:

git rebase --edit-todo

# I exited the Rebase TODO Editor to show what's left to do in the rebase.
# We could have instructed Git to edit, reword, drop, etc. the last commit.
# This is a great way to edit the rebase mid-stream beyond our initial edit. 🎉
# OK, fun digression aside, let's complete the rebase:

git rebase --continue

# Our Git log remains the same (although the last two SHAs changed due to the rebase).

# Here's the edited commit:
git show

# Finally, let's run the code to confirm our edit:

ruby calc.rb 1 2

# Here's what I love about this workflow:
# 1. We edited the original implementation and commit message while erasing our mistake.
# 2. We did all of this via the single Git Rebase Edit TODO instruction.
# 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. 🎉

# Git Rebase Edit can also aid in running tests and/or performing other actions.
# You don't get this capability when using Git Rebase Fixup or Squash.
# I leave the rest up to you to experiment further.

# Enjoy!
# ☿ 🜔 🜍 🜂 🜃 🜁 🜄