Dropping a commit, when rebasing, can seem as mundane as when you use Git Rebase Pick but, like picking a commit, dropping a commit has a slight super power too. Not quite as a fancy but powerful (more on this shortly).
Let’s say, when using Git Rebase, you want to drop a commit. Dropping a commit is effectively the same as deleting the commit from your commit history. Extremely useful when composing commits that are highly atomic that adhere to a strong Git Commit Anatomy.
While working in your feature branch, you might want to perform an interactive rebase as follows:
git rebase --interactive
The above might yield the following in your text editor:
p 2b3c42b571fe # Added calculator script file p bedbca73845e # Added calculator implementation
💡 The p is short pick due to using a Git Rebase Abbreviations configuration.
To delete the "Added calculator script file" commit, you only need to change the p to d (short for drop) as follows:
d 2b3c42b571fe # Added calculator script file p bedbca73845e # Added calculator implementation
Upon saving the above changes and exiting from the Git Rebase Editor, Git will happily complete your interactive rebase by deleting the first commit and leaving you with only the last commit ("Added calculator implementation"). This is a quick way to remove an unwanted commit.
There can be many reasons why you might need to drop a commit, here’s a few:
-
You no longer need the original change because a few commits later you found a more elegant solution.
-
Maybe the original implementation was a code spike and you needed to keep the change around until you found a better solution.
-
You don’t need the original change after learning the customer changed their mind.
-
During Code Reviews, you realized you made a mistake and don’t need the change or learned of a better solution.
Whatever the reason, Git provides a quick way to drop unwanted changes with minimal effort.
While using d is definitely handy, there a faster way which is to delete the line entirely! To illustrate, let’s look at our earlier Git Rebase Editor commands:
d 2b3c42b571fe # Added calculator script file p bedbca73845e # Added calculator implementation
Instead of changing the first line from p to d, you can delete the line entirely which means you’d have the following in your Git Rebase Editor:
p bedbca73845e # Added calculator implementation
Upon saving these changes and closing your Git Rebase Editor, Git will perform the rebase with all deleted lines dropped from your Git commit history. The effect is the same as changing p to d but with less typing.
You’re not limited to single commits. You can use d (or delete a line) for multiple commits at once. Whatever your desire, Git will notice the difference and drop the commits. Deleting lines via the Git Rebase Editor is definitely the fastest way to delete unwanted changes. That said, if you are issuing a bunch of commands (i.e. reword, fixup, drop, etc.) at once when rebasing, you might want to stick with d only to provide visual clarity before saving and letting Git complete the rebase. Up to you but knowing you have multiple ways to drop commits will speed up your workflow.
Once again, drop isn’t nearly exciting as the other Git Rebase commands but knowing you can delete lines is a powerful way to perform changes quickly. Enjoy and may your Git history remain succinct and to the point!