How to avoid “Merge branch 'master' of ssh://”

Some contributors in our project likes to work on master directly and still forget to git pull --rebase.

Is there a way to refuse on the server side commit with commit like Merge branch 'master' of ... into master ?

  • Which commit made a file executable
  • Git pull is very slow.. why?
  • NodeGit - resolve merge conflict in favor of “theirs”
  • git push crashes with “inflate returned -551”
  • Git force push tag when the tag already exists on remote
  • Is the branch after “origin” the local branch or the remote branch?
  • GIT_MERGE_AUTOEDIT=no by default
  • How to commit only a message to GIT?
  • How can I clean my .git folder? Cleaned up my project directory, but .git is still massive
  • how to create a file with the cmd?
  • Git patch of empty commits
  • Any solution about using GIT submodule between main server and local server?
  • One Solution collect form web for “How to avoid “Merge branch 'master' of ssh://””

    As VonC comments, you can do this with a simpler version of the pre-receive hook that I wrote for that question.

    To rephrase what you’re asking, you would like a pre-receive hook on your server that will refuse any push to master which has any non-linear history, i.e. introduces any commit with more than one parent. This hook should do what you want:

    #!/usr/bin/ruby -w
    ref_to_check = "refs/heads/master"
    STDIN.each_line do |line|
        rev_old, rev_new, ref = line.split(" ")
        if ref == ref_to_check
            merges_introduced = `git rev-list --merges #{rev_old}..#{rev_new}`
            unless merges_introduced.strip.empty?
                STDERR.puts "Refusing push to #{ref}, since it would create non-linear"
                STDERR.puts "history by introducing the following merge commits:"
                STDERR.puts merges_introduced

    Update: in Jefromi’s answer to the linked question, he demonstrates that using git rev-list --merges is much neater, so I’ve updated this script to use that, and fixed it to loop over every ref that the push is trying to update.

    Git Baby is a git and github fan, let's start git clone.