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

Marko Vuksanovic
Dec 3, 2016 · 2 min read

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.

Image for post
Image for post

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store