DEV Community

AmasiaNalbandian
AmasiaNalbandian

Posted on

Five birds one stone

We're all very familiar with the phrase "to kill two birds with one stone." This week I learned I could kill more than two birds with one stone by using git rebase.

Background

I've used this command previously and it resulted in a huge mess where I pulled in a bunch of commits that I did not make in a branch, and then all those commits got added to my PR.

This week I learned exactly why that happened by actually understanding what git rebase does.

Lab 05

For Lab 5 I was more than happy to see we could use the time to refactor our code for the static site generator(SSG). I discussed my struggles setting up the SSG in release 0.1 and with that came a lot of bugs and what I will refer to as "working" code(to say that it could be better is an understatement).

After my experience from release0.1, I was lucky and grateful to have David and Reza log in some issues on top of the ones I discovered. I decided to use these to refactor the code.

Commits

Below are the issues I decided to use for commits to then squash.

Adding protection to invalid arguments in the cli
Fixing the mismatch license
Fixing incorrect variable types
Improve error handling

Outcome

After creating commits for each of the issues, I then ran an interactive git rebase by using the following command:

git rebase main -i

To my surprise there was a lot of interaction in the CLI through Vi, and it threw me off as I tried to understand what I was supposed to do. This is the image of the message in the editor:
Image of editor after running interactive rebase command

After a few seconds I realized what I had to do was change the text from pick to squash to squash all five commits into one.

Image of squash text in editor

Once this was done I received the following:

Image of result after rebasing and squashing five commits

I then went ahead and I checked out back to main, and merged my changes from the refactoring branch. I learned another thing here: My local commits from the refactoring branch were able to be merged into the main branch which was in GitHub. I had never tried this before without putting both branches up on GitHub, which explains why I was probably a bit shocked.

Lastly, I wanted to attach this image below which would demonstrate how a rebase works.

And another image here, which explains the difference between a rebase and a merge (as I know most people use these methods to grab latest commits from main branches in their feature branch.... cough cough me).

Image description

Top comments (0)