Git proficiency with rebase

I am a developer now for more than 10 years. Except a few years with SVN, I was solely working with Git. The best quote I got from one of my CTOs in the early days, was:

“Git is like the whole world, but we need just the size of a lake of it.”

You mainly use:

That’s mostly it, right? Creating new branches, pushing them to Bitbucket or GitHub, and then merge them via the GUI of one of the two websites.

My latest project let me work with around 7 developers, and we all work on the same project, and on multiple GitHub issues at once. There, a simple “git merge develop MYBRANCH” didn’t cut it anymore. More was needed. And there I found it, the holy command of the year 2017. At least of my version of 2017.

“git rebase -i HEAD~3” and other funny things.

A colleague in the current project introduced me to a whole new world of telling “stories” with your git commits. Grouping them together so the reviewer gets a sense of what is going on and for which reason. Back then, in the old days, I would just commit around 10 times, push it all to GitHub and the reviewer went file for file and try to understand what’s going now.

Now though, I group commits together which belong together. So the reviewer can go commit by commit through my pull request and understand what’s going on.

An example

I mainly work with React in the frontend is the current project, so lets grab an issue from GitHub which is called “Add a switch component to the User registration form”. The steps we will complete the task is:

So we have around 3-4 commit with this pull request, everything is mixed up and from the timing of the commits and the grouping of the resulting code, it’s really hard to read. So here it comes, our rescue:

git rebase -i HEAD~4

Before we move on, we have to make sure that our current “develop” branch is up to date. Therefore we:

(git pull works differently then git pull origin develop. With git pull, we’ll get the whole history of every branch, also from the remote ones.)

and then, checkout our feature branch again:

And now comes the magic. We can group different commits together with the use of git rebase -i HEAD~4. What this does, is:

Here is a nice overview of all the options you are given there.

Now you can pick and group commits together with:

What does it bring you?

Well, it’s a nice idea to group commits together which belong together, so the reviewer of your code has an easier job going through each step and make a better sense of it. Also, your git history stays super clean and readable.

Think about a super way of documenting your process and code. Once you figured out the mechanics, it’s a huge boost in professionality and quality.