How to avoid merge-commit hell on GitHub/BitBucket

Rebase Feature Branches Before Merging

If you want to avoid merge commits, you need to ensure all commits are fast-forwards. You do this by making sure your feature branch rebases cleanly onto your line of development before a merge like so:

git checkout master
git checkout -b feature/foo

# make some commits

git rebase master
git checkout master
git merge --ff-only feature/foo

Rebase also has a lot of flags, including interactive rebasing with the -i flag, but you may not need that if you’re keeping things as simple as possible and want to preserve all of your branch history on a merge.

Use the --ff-only Flag

Aside from rebasing, the use of the --ff-only flag will ensure that only fast-forward commits are allowed. A commit will not be made if it would be a merge commit instead. The git-merge(1) manual page says:

–ff-only

Refuse to merge and exit with a non-zero status unless the current
HEAD is already up-to-date or the merge can be resolved as a
fast-forward.

Leave a Comment