Git tips: Use only fast-forward merges (with rebase)

When working with git there are two ways in which a branch can make it’s way into master branch. One way is to use “classic” git merge. The other way is to use “rebase” model. I personally prefer the “rebase” model as it keeps your history clean and reverting any commit (or a series of commits) is a breeze. If you’re not clear what is “rebase” merge model or what are pros and cons of using each of the models here is a great article on that topic.

When using “rebase” model, your working branch is always rebased onto master (or refs/origin/master). Only after the rebase is complete you merge that branch into master branch. To avoid accidents where you forgot to rebase branch change your git config (located at $HOME/.gitconfig) so that it includes the following lines:

[branch "master"]
mergeoptions = --ff-only

Alternatively you can use git config to specify default merge options for a branch (branch.<remote>.mergeOptions property):

git config --add branch.master.mergeoptions --ff-only

This tells git to allow merges into master only if merge is afast-forward merge. Following is a great explanation ( of what a fast-forward merge is.

I hope this saves you from those situations where you end up accidentally merging code into master when using “rebase” merge model.