Set default merge options for Git branches



Time for a little Git tip!

Suppose you are the integrator of a branch. It is your responsibility to merge different branches from your co-workers. You want to keep a linear history. I.e. you want to avoid this:


Simply configure your master branch like this:
git config branch.master.mergeoptions "--ff-only"
Now every time you want to merge a branch that does not create a linear history, you will get the following (rather direct) message:
fatal. Not possible to fast-forward, aborting.
Now you can rebase that branch and merge it again (or tell your co-worker to do that for you, the lazy git).

You can configure multiple options:
git config branch.master.mergeoptions "--ff-only –-squash"
This configures the branch so that it only accepts a linear history and squashes all commits into a single new commit.

Want to remove these merge options? Simply do:
git config --unset branch.master.mergeoptions
Remember that these options are local for your Git repository. You have to repeat them for every Git repository you clone.

Have fun and happy coding!

Comments

  1. I haven't tried the following myself, but I've read on this Stack Overflow answer that you actually can set --ff-only in a global manner. You just have to take a slightly different approach:

    git config --add merge.ff only

    "When set to only, only such fast-forward merges are allowed (equivalent to giving the --ff-only option from the command line)."

    ReplyDelete
  2. When I said globally, I meant for all branches, not for all repositories. This would avoid repeating it for branch.master as well as, for one example, branch.development when merging feature branches. I still haven't found a --global sort of option to set this for all repos.

    ReplyDelete
  3. I actually quite like the flexibility to only set it on a branch. I usually just set it on my master branch. I don't care about the other branches.

    ReplyDelete

Post a Comment

Popular posts from this blog

Small tip: use QueueBackgroundWorkItem for asynchronous work in ASP.NET

Why you should use git merge --no-ff when rebasing.

Using NUnit for your tests in Team Foundation Service with a Git repository