Git – Take care of your branches

Commit messages are not only for you – they are mainly for your colleagues and anyone who will work with your code in the future. That is the reason why we all should care about what we are communicating in each single message. After some period of time “bug fix” or “new feature” means nothing, so make sure that your commits are clear and they are actually telling the truth about changes that you committed.

Keeping git log in a good shape is not an easy task, especially when you are trying to fix some nasty bug overnight. Git was made for programmers, so it has some built-in tools that are really helpful when it comes to managing your feature branch log. In this article, I will try to present some common situations when our feature branches may become little messy and how to avoid them.

Forgotten files

You created a template for an e-mail message, but you have forgotten to commit some asset. Currently, your git log looks like this:

017dc1d Email template for order confirmation.
3968fa3 Add a missing attribute to Order.

Your most recent commit is 017dc1d and you want to add the missing asset to it. Git newbie probably would add the missing asset in the separate commit. That is not the best solution, as the log from now on is polluted with unwanted commit:

Do we really need three separate commits for tiny and quick changes applied to web page header? Does it bring any value for the future development? I don’t think so. We can squash those three entries into one elegant commit with a relevant message.

In order to do squash, we need to enter interactive rebase mode for our last 3 commits:

Incorrectly named feature branch

Let’s assume that some developer needs to add a new payment method to client’s system. He starts from creating a new branch called: feature/wallet-payments . After couple days he presents code to his manager in order to receive feedback. It turns out that system already has module called “Wallet”. To not confuse other team members it would be great to replace every occurrence of word “wallet” with something different. Once all changes are introduced the developer should not forget about changing the name of the feature branch. It can be done in three steps.

First, we need to change a name of the branch locally:

git branch -m old_branch new_branch

Then let’s delete old branch:

git push origin :old_branch

And finally we can push new branch and set it’s tracking:

git push --set-upstream origin new_branch

Typo in the commit message

One of my feature branch looks like this:

11d47da Fix missing body open tag
37cbb8a Increment bower verison

In the second commit, I obviously made a typo and wrote: “verision” instead of “version”. I do not want anybody to think that I am a careless person so let’s get rid of this typo. Once again the interactive rebasing tool comes in to play:

Save the file, edit the message and save again. Now our commit messages are free of typos.

Artur Chmaro

Full-stack Developer.

Artur is a full-stack developer currently living in Szczecin, Poland. His favorite technologies are Ruby on Rails, NodeJS and React. Founder of the biggest Polish portal about MMO games and new e-sport gaming portal. When not working, Artur is often spending his time on bike trips and reading fantasy books.