---
title: "Do not allow force push to master"
description:
  "Force push is dangerous. But it is needed sometimes. And sometimes one can
  force push to master by mistake. A look at how to avoid that."
canonical_url: "https://www.bigbinary.com/blog/do-not-allow-force-push-to-master"
markdown_url: "https://www.bigbinary.com/blog/do-not-allow-force-push-to-master.md"
---

# Do not allow force push to master

Force push is dangerous. But it is needed sometimes. And sometimes one can force
push to master by mistake. A look at how to avoid that.

- Author: Neeraj Singh
- Published: September 19, 2013
- Categories: Misc

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](https://www.bigbinary.com/how-we-work) 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](https://github.com/bigbinary/tiny_scripts/blob/master/git-hooks/hooks/pre-push).
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.

```plaintext
*************************************************************************
Your attempt to FORCE PUSH to MASTER has been rejected.
If you still want to FORCE PUSH then you need to ignore the pre_push git hook by executing following command.
git push master --force --no-verify
*************************************************************************
```

## Requirements

`pre-push` hook was
[added to git](https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt#L167)
in version 1.8.2. So you need git 1.8.2 or higher. You can easily upgrade git by
executing `brew upgrade git` .

```plaintext
$ git --version
git version 1.8.2.3
```

## Setting up hooks

In order for these hooks to kick in they need to be setup.

First step is to clone the [repo](https://github.com/bigbinary/tiny_scripts) to
your local machine. Now open `~/.gitconfig` and add following line.

```plaintext
[init]
  templatedir= /Users/neeraj/code/tiny_scripts/git-hooks
```

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

Now `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.

```plaintext
$ git init
Reinitialized existing Git repository in /Users/nsingh/dev/projects/streetcommerce/.git/
```

Now if you look into the `.git/hooks` directory of your project you should see a
file called `pre-push`.

```plaintext
$ ls .git/hooks/pre-push
.git/hooks/pre-push
```

It means this project is all set with `pre-push` hook.

## New repositories

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

To ignore `pre-push` hook all you need to do is

```plaintext
# Use following command to ignore pre-push check and to force update master.
git push master --force --no-verify
```

[The hook is here](https://github.com/bigbinary/tiny_scripts/blob/master/git-hooks/hooks/pre-push)
.

## Links

- [Human page](https://www.bigbinary.com/blog/do-not-allow-force-push-to-master)
