Interactive Rebase: A Powerful Git Feature for Beginners and Intermediates 🎯

beginner
25 min

Interactive Rebase: A Powerful Git Feature for Beginners and Intermediates 🎯

Git is a powerful version control system, and one of its most useful features is Interactive Rebase. This feature allows you to modify, squash, or edit your commit history in a clean and interactive way. Let's dive in and learn how to use it! 📝

Understanding the Basics 📝

Before we start, let's quickly review some Git terminologies:

  • Commit: A snapshot of your code at a specific point in time.
  • Branch: A separate line of development in your project.
  • Rebase: A Git operation that moves or combines commits onto a new base commit.

Starting the Interactive Rebase 💡

  1. First, ensure you're on a feature branch or a branch with commits you'd like to rebase:
bash
git checkout my-feature-branch
  1. Now, you can start the interactive rebase by specifying the base branch:
bash
git rebase -i main

Replace main with the name of the base branch in your project.

The Interactive Rebase Menu 💡

Upon running the above command, Git will present you with an editor containing a list of commits along with the words pick, reword, edit, squash, fixup, and drop. Each line represents a commit.

  • pick: Select the commit to be applied as is.
  • reword: Edit the commit message.
  • edit: Edit the commit changes.
  • squash: Combine the commit with the previous one and edit the commit message.
  • fixup: Combine the commit with the previous one and use the previous commit message.
  • drop: Remove the commit.

Practical Example 💡

Let's practice with an example. Suppose we have the following commit history:

bash
A - B - C - D - E - F (main) \ G - H (my-feature-branch)

Now, you want to squash G and H into one commit and reword D's commit message.

  1. Start the interactive rebase:
bash
git checkout my-feature-branch git rebase -i main
  1. You'll see the following in your editor:
pick A pick B pick C pick D pick E pick F pick G pick H
  1. Change the first pick for G to squash and the second pick for H to f (for fixup):
pick A pick B pick C pick D pick E pick F squash G fixup H
  1. Save and close the editor. Git will open the editor again, asking you to edit the commit message for the squashed commit (G and H). Save and close the editor again.

  2. Git will then automatically apply the changes. You'll see a new commit representing the squashed G and H with the modified commit message from the second editor session.

Pro Tip 💡

Use the reword command if you want to change a commit message without squashing or combining commits.

Quiz 🎯

Quick Quiz
Question 1 of 1

What does the `pick` command represent in the interactive rebase menu?

Wrapping Up 📝

In this tutorial, we learned about Interactive Rebase, a powerful Git feature that lets you modify your commit history in a clean and interactive way. Practice with small projects to gain a better understanding of this feature and unlock the full potential of Git for your projects! 💡