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! 📝
Before we start, let's quickly review some Git terminologies:
git checkout my-feature-branchgit rebase -i mainReplace main with the name of the base branch in your project.
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.Let's practice with an example. Suppose we have the following commit history:
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.
git checkout my-feature-branch
git rebase -i mainpick A
pick B
pick C
pick D
pick E
pick F
pick G
pick H
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
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.
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.
Use the reword command if you want to change a commit message without squashing or combining commits.
What does the `pick` command represent in the interactive rebase menu?
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! 💡