11

Is it possible to merge only fast-forwardable branches and create a merge commit? I want to rebase the branches before merging but still have the branch separately in history. So, I'm wondering if there is some way to make sure that git won't merge if the branch hasn't been rebased (--ff-only) but at the same time create a merge commit (--no-ff). Giving both --ff-only and --no-ff doesn't work.

Motivation: I want to have linear history but so that each feature branch is clearly separate. They just follow one after another.

Jaakko Luttinen
  • 695
  • 2
  • 7
  • 21
  • To conclude: I suppose git-merge doesn't support such behaviour, one has to use a custom script/alias. This seems like a bad design decision to me, because the two matters (1. require fast-forwardable input branch and 2. always create a merge commit) are orthogonal. For more discussion, see: http://git.661346.n2.nabble.com/PATCH-merge-allow-using-no-ff-and-ff-only-at-the-same-time-td7590816.html – Jaakko Luttinen May 07 '16 at 07:29

2 Answers2

10
git config alias.mff '!mff() { git merge --ff-only "$1" && git reset --hard HEAD@{1} && git merge --no-ff "$1"; }; mff'

and from then on

git mff other-branch
Vampire
  • 35,631
  • 4
  • 76
  • 102
  • Is it possible to use it also over already pushed branches? – Kamafeather Jul 25 '18 at 00:30
  • 1
    What do you mean "use it over"? It just verifies that a ff-only merge would succeed and then does a normal merge. Doesn't matter whether branches are pushed or not. – Vampire Jul 25 '18 at 00:34
-1

With the motivation being to have linear history but each feature branch separate, it sounds like you might want to explore rebasing. You should be able to combine git rebase and git merge to get a similar effect.

It can be a bit dangerous to rewrite history of commits with rebase depending on the workflow of your group, but it is an alternative option.

Information from ProGit.

Briana Swift
  • 1,047
  • 8
  • 9
  • Yes, thanks, I'm doing rebasing before meging, but I'd like to force git-merge to merge only if I have rebased. `--ff-only` to force rebased merges only and `--no-ff` to foce merge commit. – Jaakko Luttinen May 07 '16 at 07:00
  • Okay, I think I'm understanding more...Have you looked into `git config merge.ff only` or `git config merge.ff false`? [Git docs](https://git-scm.com/docs/git-config) It may not be exactly what you're looking for, but I'm not positive git has configs or commands to do that. You're right that you may need to write your own script. – Briana Swift May 09 '16 at 13:10