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.


"Todd A. Jacobs" already mentioned "rebase" is the concept here. This is just a more detailed way of doing things.

Let's say you're on the master branch

$ git branch
  * master

You want to make a fix, so create a “fixbranch” which is branched from the master

$ git checkout -b fixbranch

Maybe you would have worked for a couple of days on this branch and had a couple of commits.

The day you wanted to push your commits to central master repo! Checkout master and get the latest changes from the central master repo

$ git checkout master
$ git pull origin master

Rebase your fixbranch with the master to have a clean history and resolve the conflicts if any in the local repo itself.

$ git checkout fixbranch
$ git rebase master

Now fixbranch is uptodate to with the central master, let me merge fixbranch into the master branch

 $ git checkout master
 $ git merge fixbranch

I’m done! let me push local master to the central master

$ git push origin master

https://git-scm.com/book/en/v2/Git-Branching-Rebasing