git push force

I have heard horror stories about developers accidentally force pushing to the master branch of their project. This can be very difficult to revert, but luckily this can be avoided using a Git pre-push hook.

A pre-push hook is just a script that lives in the .git/hooks/* directory. It is run every time you push to your Git repository. I have edited the script to prompt you for a confirmation before you push to the master branch. Are you sure you want to push to "master" ? (y/n):

To get this to work:

  1. Add the code below to you project’s .git/hooks/pre-hook file
  2. Make the script executable with chmod +x .git/hooks/pre-hook
  3. Try this out my making a minor change in one of the directories above and git push
  4. (Optional) If you want this hook in all git projects by default, try out git templating.

The code: