8

I would like to disable git's --force flag usage on local machine completely (globally for all the repos I have). I assume it's possible with some bash hook, but where can I read more info about creating one?
Ideally the behavior would be like this: every time I try to use --force flag in any push command in any repository - my terminal will show the command abortion and tell me that it's forbidden.

UPDATE: Unfortunately none of answers are correct because I don't need a repository-located hook. I need a hook on my local computer to prevent me from using --force flag when pushing to remote.

konnigun
  • 1,797
  • 2
  • 17
  • 30
  • 4
    Did you check [this](http://stackoverflow.com/questions/1754491/is-there-a-way-to-configure-git-repository-to-reject-git-push-force)? – Jan Aug 04 '15 at 08:26
  • You may find [this](https://gist.github.com/pixelhandler/5718585) useful – Gaurav Tiwari Aug 04 '15 at 11:09

1 Answers1

4

The receive.denyNonFastForwards config option can do what you want.

receive.denyNonFastForwards

If set to true, git-receive-pack will deny a ref update which is not a fast-forward. Use this to prevent such an update via a push, even if that push is forced. This configuration variable is set when initializing a shared repository.

Set this in your personal git configuration with

git config --global receive.denyNonFastForwards true
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
  • 2
    Would this block --force-with-lease as well? I'm trying to condition myself to always force with lease but... habits... – Pluc Jul 25 '18 at 18:46
  • @Pluc this is exactly the reason I'm here, did you find a solution? – dreua Dec 10 '21 at 07:07