Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:01:05+00:00 2026-05-28T06:01:05+00:00

A while back I asked our developers to use rebase instead of merge before

  • 0

A while back I asked our developers to use rebase instead of merge before pushing. Eliminating trivial merges makes for a much easier to follow commit graph (ie: gitk, git log).

Sometimes folks still accidentally do trivial merges, then push. Does anyone have handy or have tips for writing a server-side hook that blocks trivial merges?

By “trivial merge”, I mean a merge without conflicts. Here’s an example, and here’s a better explanation of a trivial merge in git.

Update Wed Nov 10 01:26:41 UTC 2010: great comments, all! Thank you.

  • Consider the following: all I’m really asking folks to do is this:
    • if git pull --ff-only fails, do git pull --rebase instead of git pull
  • git.git only has one or two committers, right? In theory, it should be easy to follow the commit graph, but it looks pretty messy to me.

Update Thu Nov 11 23:49:35 UTC 2010:

  • here’s a gist with some in-progress work
  • another idea is to prevent trivial merges on the client end, with a “git push” wrapper

Update Wed Dec 15 18:34:52 UTC 2010:

  • adymitruk is close! Just one case is still unresolved: non-trivial merges must still work.
  • A rather complete test suite is available, check it out.
  • I asked for help on a (the?) git mailing list.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:01:06+00:00Added an answer on May 28, 2026 at 6:01 am

    I came across this piece of code, while trying to find a solution. It doesn’t do exactly what you want, but it should be ez to add extra branch names on the if statement.

    Works for me, so far. it forces pull –rebase for the same branch and lets regular merges with other branches go through.

    All credits go to the original author.

    #!/bin/bash
    #
    # This git update hook will refuse unnecessary merge commits caused by pulling
    # from being pushed to a shared repository. These commits make following the
    # history of a project difficult and are always avoidable with rebasing.
    #
    # by Scott Kyle (appden)
    # modified by Olivier Refalo (orefalo)
    
    refname="$1"
    oldrev="$2"
    newrev="$3"
    
    # if this is not run as a hook, you may have messed up
    if [ -z "$GIT_DIR" -o -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
        echo "Usage: GIT_DIR=<path> $0 <ref> <oldrev> <newrev>" >&2
        exit 1
    fi
    
    # if the new revision is all 0's then it's a commit to delete a ref
    zero="0000000000000000000000000000000000000000"
    # also check if the new revision is not a commit or is not a fast forward
    # detect branch deletion, branch creation... and more
    if [ "${refname#refs/heads/}" = "master" ] || [ "$newrev" = "$zero" ] || [ "$oldrev" = "$zero" ] || [ $(git cat-file -t $newrev) != "co
    mmit" ] || [ $(git merge-base $oldrev $newrev) != "$oldrev" ]; then
        exit 0
    fi
    
    # loop through merges in the push only following first parents
    for merge in $(git rev-list --first-parent --merges $oldrev..$newrev --); do
        # lazily create the revision list of this branch prior to the push
        [ -z "$revlist" ] && revlist=$(git rev-list $oldrev)
        # check if the second parent of the merge is already in this branch
        if grep -q $(git rev-parse $merge^2) <<< "$revlist"; then
            cat >&2 <<-EOF
                *** PUSH REJECTED ***
                *** TRIVIAL merge detected on local branch ${refname#refs/heads/}
                *** To fix: git rebase origin/${refname#refs/heads/}
                ***
                *** Next time use: git pull --rebase
                ***
                *** Permanent fix: git config [--global] branch.autosetuprebase always
                *** Then for existing branches: git config branch.<name>.rebase true
            EOF
            exit 1
        fi
    done
    
    echo -Info- Clean history successfully preserved!
    exit 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was working with a new C++ developer a while back when he asked
So I asked a question a while back about securing downloads using C# (
I asked this question a while back but now I'm looking to implement an
I asked a question a while back on here regarding caching data for a
A while back a found a great-looking framework that allowed .net developers to implement
A while back, I asked a question on here about how to run an
I asked a similar question here a while back but all the answers were
Ok so while back I asked question Beginner ASP.net question handling url link I
A while back I asked link text and I got some great answers. What
A while back (freshman year of high school) I asked a really good C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.