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.
If you have the appropriate permissions on your "central" git repository, you can disable force push by git config --system receive. denyNonFastForwards true .
To find it go to Settings > Branches > Branch Protection Rules and click 'Add Rule'. Then, enter the name of the branch you want to protect and click the checkbox to require pull request reviews before merging. By default, this only stops people who are not moderators.
To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch). See the <refspec>... section above for details. Force an update only if the tip of the remote-tracking ref has been integrated locally.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With