Git - Dropping Commits Using Rebase

July 16, 2023

In our last few posts, we've covered using the rebase command to reorder commit history, squashed commits to combine multiple commits into one singular commit, and also split one single commit into multiple commits.

 

In this post, we'll cover dropping a commit from the Git history. This operation also uses the rebase command. But using the rebase command comes with a caveat.

#1 Rule to Rebase: Don't use it if you don't know exactly what you're doing!

The rebase command is a powerful but potentially dangerous tool, because it has the ability to rewrite commit history. If not used properly, it can cause data loss, cause headaches for team members, and reorder the logical development history of the project which can cause confusion. Use with care!

 

The first step we'll take is to add a folder to our desktop named "drop". We'll change into that directory using the "cd" command. Then we'll use the "git init" command to initialize Git in the local folder.

 

From there we'll add a file called "test.js" and commit it locally by using the following commands.

We'll now delete the file from the directory using the command "git rm test.js" You could also just use "rm test.js" and it would have the same effect. Then we'll use the command to add everything to the Git tracker, then make a new commit.

Just to summarize, we added a file and committed it. Then we deleted that file and committed again. What should the git log look like now, and what about the file in the folder?

 

Here is what the git log shows:

We can see we have both commits there, one with the addition of "test.js" and one with a deletion of "test.js".

 

At this point, my file folder will no longer contain "test.js" because we just deleted the file. But what if we really wanted that file back the way it was before the deletion? There's a solution for that, using rebase!

 

The command to use while you're in the working directory is "git rebase -i HEAD~1". This will take us to the following screen:

We can see we have chosen the correct commit to delete. We'll hit the "i" key in the Vim Editor to edit the text, and we'll change "pick" to "drop". We'll then use the commands [ESC] :wq to exit out of writing mode, save the edited text and exit back to the command line. This is what we see:

We now see confirmation that the rebase was successful and that the last commit was dropped.

Now let's examine the folder using the "ls" command to list the contents of the directory.

The file "test.js" is back! Like magic, Git took us back in time to our previous commit! It's like our deletion never happened.

 

See the new git log:

Here we can see that the "Delete test.js" commit has been removed.

 

This is an effective way to move backwards one commit. If for example, you introduced a really tricky bug that you already committed and it's not too difficult to re-work the solution, maybe starting from the previous commit would be appropriate in that case.

 

Thanks and stay tuned for more on Git!