At BigBinary we create a branch for every issue. We deploy that branch and only when it is approved that branch is merged into master.
Time to time we rebase the branch. And after rebasing we need to do
force push to send the changes to github. And once in a while someone
force pushes into master by mistake. We recommend to set push.default to current to avoid such issues but still sometimes force push does happen in master.
In order to prevent such mistakes in future we are using pre-push hook. This is a small ruby program which runs before any
git push command. If you are force pushing to
master then it will reject the push like this.
pre-push hook was added to git in version 1.8.2. So you need git 1.8.2 or higher. You can easily upgrade git by executing
brew upgrade git .
Seting up hooks
In order for these hooks to kick in they need to be setup.
First step is to clone the repo to your local machine. Now open
~/.gitconfig and add following line.
Change the value
/Users/neeraj/code/tiny_scripts/git-hooks to match with the directory of your machine.
Making existing repositories aware of this hook
pre-push hook is setup. Any new repository that you clone will have the feature of not being able to force push to master.
But existing repositories do not know about this git-hook. To make existing repositories aware of this hook execute following command on all repositories.
Now if you look into the
.git/hooks directory of your project you should see a file called
It means this project is all set with
When you clone a repository then
git init is invoked automatically and you will get
pre-push already copied for you. So you are all set for all future repositories too.
How to ignore pre-push hook
pre-push hook all you need to do is