How can I prevent foxtrot merges in my 'master' branch?


A foxtrot merge is a merge where ‘origin/master’ merges in as a 2nd (or later) parent, like so:

Commit 'D' is a foxtrot merge because 'origin/master' is its 2nd parent.

Commit ‘D’ is a foxtrot merge because ‘origin/master’ is its 2nd parent. Notice how first-parent history from ‘origin/master’ contains commit ‘B’ at this moment.

But in my git repo I need all merges involving ‘origin/master’ to keep ‘origin/master’ as the 1st parent. Unfortunately git doesn’t care about parent-order when it evaluates whether a commit is eligible for fast-forward. This causes the first parent history on my master branch to sometimes lose commits that used to be there (e.g., output of “git log –first-parent”).

Here’s what happens when commit ‘D’ from the earlier diagram is pushed:

How can I prevent this push? First-parent history of 'origin/master' no longer contains commit 'B' after the foxtrot merge is pushed!

How can I prevent this push? First-parent history of ‘origin/master’ no longer contains commit ‘B’ after the foxtrot merge is pushed!

Obviously no commits or work are actually lost, it’s just that in my environment I really need “git log –first-parent” to be a stable accumulative record of commits – if you like, a kind of “Write-Once Read-Many” (WORM) database. I have scripts and processes that use “git log –first-parent” to generate changelogs and release notes, as well as to manage ticket transitions in my ticketing system (JIRA). Foxtrot merges are breaking my scripts!

Is there some kind of pre-receive hook I could install in my git repositories to prevent foxtrot merges from getting pushed?

p.s. The commit graphs in this stackoverflow question were generated using


The following pre-receive hook will block those:


# Copyright (c) 2016 G. Sylvie Davies.
# Copyright (c) 2016 torek.
# License: MIT license.
while read oldrev newrev refname
if [ "$refname" = "refs/heads/master" ]; then
   MATCH=`git log --first-parent --pretty='%H %P' $oldrev..$newrev |
     grep $oldrev |
     awk '{ print \$2 }'`

   if [ "$oldrev" = "$MATCH" ]; then
     exit 0
     exit 1

If you’re using Github / Gitlab / Bitbucket Cloud, you may need to look into creating some kind of call into their commit status apis (here’s api docs for: bitbucket, github; not sure if gitlab has one), because you don’t have access to the pre-receive hooks, and even if you did, you’d still have to deal with people clicking the “merge” button directly in the web ui of those products (in which case there is no “push”).

With Bitbucket Server you can install the add-on I created.

Once it’s installed you click “enable” on the “Protect First Parent Hook” in a given repository’s “hook” settings:

enter image description here

It will block foxtrot merges via push and via the “merge” button in the Bitbucket Server UI. It does this even if its license is expired, making the “Protect First-Parent Hook” a free component of the larger add-on.

Here’s an example of my Bit-Booster “Protect First Parent” pre-receive hook in action:

$ ​git pull
$ git push

remote: *** PUSH REJECTED BY Protect-First-Parent HOOK ***
remote: Merge [1f70043b34d3] is not allowed. *Current* master must appear
remote: in the 'first-parent' position of the subsequent commit. To see how
remote: master is merging into the wrong side (not as 1st parent), try this:
remote:   git show --graph -s --pretty='%h %d%n' \
remote:      1f70043b34d3 1f70043b34d3~1 origin/master
remote: To fix, there are two traditional solutions:
remote:   1. (Preferred) rebase your branch:
remote:       git rebase origin/master
remote:       git push origin master
remote:   2. Redo the merge in the correct direction:
remote:       git checkout master 
remote:       git reset --hard origin/master 
remote:       git merge --no-ff 1f70043b34d3eaedb750~1
remote:       git push origin master

For more background on foxtrot merges I wrote a blog post.

Answered By – G. Sylvie Davies

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published