konnigun
konnigun

Reputation: 1805

Disable `git push --force` locally

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.

Upvotes: 8

Views: 1533

Answers (1)

Greg Bacon
Greg Bacon

Reputation: 139671

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

Upvotes: 4

Related Questions