Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable `git push --force` locally

Tags:

git

bash

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.

like image 727
konnigun Avatar asked Aug 04 '15 08:08

konnigun


People also ask

How do I turn off force push?

If you have the appropriate permissions on your "central" git repository, you can disable force push by git config --system receive. denyNonFastForwards true .

How do I stop master pushes?

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.

Can you force a git push?

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.


1 Answers

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
like image 127
Greg Bacon Avatar answered Oct 08 '22 20:10

Greg Bacon