Is there a way to configure git repository to reject 'git push –force'?

I was wondering is there a way to prevent ‘git push --force‘ on a repository (only on master branch)?

Assume I have remote git repository and do:

  • How to create a Git alias with nested commands with parameters?
  • Why do so many projects prepend “v” to the git version tags?
  • How to make fork changes reflect with the master when updated?
  • Git commit bash script
  • How to get 3-way merge in GIT on non-conflict merges?
  • eslint pre-commiot on git
    • git push‘ to ‘master’. It works.
    • git push --force‘ to ‘branch-1’. It works.
    • git push --force‘ to ‘master’. It is rejected.

    Is it even possible?

    Thanks for any answers and suggestions.

    BR,
    Dawid.

  • Git reset --hard not working as expected
  • Why does git delete ignored files under `directory/*` but not under `directory/`
  • git checkout to restore files from a prior branch: pathspec did not match any file(s) known to git
  • Why is merging feature branches into release branches a bad idea?
  • How to view diff of a forked github project
  • Git - Ignoring a specific modification to a config file
  • 2 Solutions collect form web for “Is there a way to configure git repository to reject 'git push –force'?”

    Setting the configuration variables:

    receive.denyNonFastForwards
    receive.denyDeletes
    

    will prevent any ‘forced’ pushes from working across all branches.

    If you want finer pre-branch control then you will have to use a ‘hook’ on the remote repository, probably the ‘update’ hook.

    There is a sample update hook called ‘update-paranoid’ that probably does what you need (and more) in the git distribution in the ‘contrib’ folder.

    gitweb link

    I wrote this quick update hook to prevent non-fast-forward updates (pushes) on the “dev” branch in a repository:

    #!/bin/sh
    
    REFNAME=$1
    OLDSHA=$2
    NEWSHA=$3
    
    if [ "refs/heads/dev" != $REFNAME ]; then
      exit 0
    fi
    
    MERGEBASE=$(git merge-base $OLDSHA $NEWSHA)
    if [ $OLDSHA = $MERGEBASE ]; then
      exit 0
    fi
    
    echo "Not a fast-forward on branch dev"
    exit 1
    
    Git Baby is a git and github fan, let's start git clone.