I wanna take it for granted; I'm not pushing to master

Published on by Dan KlcoPicture of Me Dan Klco

Sometimes when I accidentally push to the wrong branch in git, things get so crazy I want to head to the beach and play guitar at you:

I just want to push code around,
Well, I will, well, I will
I don't wanna push to master
No, I don't, no, I don't
I wanna take it for granted
Yeah, I wanna take it for granted
Well, I will, well, I will
But seriously, there are few things more annoying than realizing that you accidentally pushed code to the main / master branch instead of a PR branch, especially when branch protection is not enabled for a repository.

Frustratingly, this has happened to me a few times recently, so I wrote a pre-push hook to warn me if I'm about to push into a branch with a name containing main, master or release.

The hook is enabled globally, so if I'm working on a project and try to push to main, I get a warning like:

Screenshot of the Pre-Push warning

As it's a warning, I can still allow the push to continue by pressing Y, but most of the time the warning is sufficient to avoid pushing to the wrong branch by accident.

How do I get it?

You can install the hook from the Github Gist with the following commands:

mkdir -p ~/.githooks
curl https://gist.githubusercontent.com/klcodanr/9cb36294d9e76c0b392b3209241ad900/raw/915f2d42d8dec5a52e1ce72b69e22d17925d6356/pre-push --output ~/.githooks/pre-push
chmod +x ~/.githooks/pre-push
git config --global core.hooksPath ~/.githooks/

Want to try it out first? You can run this Dockerfile to demo the functionality in the ~/local folder:

docker build . -t hook-test
docker run -it hook-test /bin/bash

Hopefully you find the pre-push hook as useful as I do. While ideally this is also enforced at the repository level, but especially when working on multiple legacy projects you can never be sure how the repositories are configured.


comments powered by Disqus