Pro Tip #1: Check yourself before you wreck yourself

Robert Herber
Kingstinct
3 min readMar 21, 2017

--

This post is leaning slightly to the technical side. If you know what Git is you’ll probably get something out of it. It’s a simple trick that eliminates a lot of simple mistakes - so you and your team can focus more on delivering quality products and less time cursing.

Tip: use git pre-push hooks to automatically run tests and style checks before pushing code

Let me elaborate on the value this provides. Of course it depends a lot on the quality of the tests it runs — but even a simple style check that you always run will eliminate lots of headaches. With this — you just won’t be able to push code with a typo or syntax error.

Why not use pre-commit hooks instead of pre-push? Sure, if it suits your workflow better — go ahead. For me and my team — I find that it might get in the way of our workflow. Sometimes you still want to commit code that doesn’t pass all checks you set up. This doesn’t mean you should push it.

I also prefer limiting the check to the master branch. Why? Once again it suits our workflow better. It’s important to be able to push unfinished features to other branches without perfecting everything. Of course it should be fixed before it’s merged back into the master branch — and our pre-push hook will help with that.

Key take aways

  • Use git pre-push hooks to run tests and style checks before you push code
  • Just a simple linter will provide much value for little effort — but with that said your pre-push hook is never better than your tests and checks
  • I recommend restricting the check to the master branch to keep it flexible.

How to set it up

I’ll show you how I’ve set it up for my node environments — if you’re not using node you could still use the main part which is the pre-push script:

[ \”$(git rev-parse — abbrev-ref HEAD)\” != \”master\” ] || (git stash create pre-push && npm test) >&2

The “npm test” part is the script I’m running to validate my code — replace it with whatever suits your project.

  • The first part checks whether your master branch is checked out — and skips the rest if not
  • The second part (git stash create pre-push) ensures that the tests are run without any uncommited changes. This is important. Otherwise you’ll risk pushing different code than your tests run on.
  • The third part (npm test) is as mentioned the tests you’re using.
  • The fourth part is outputting results to STDERR — which is needed for some git clients to output errors.

So to set up this in a node environment we’ll do the following:

npm install --save-dev pre-push

or

yarn add --dev pre-push

After this you just have to add this to your package.json:

…,
“scripts”: {

“pre-push”: “[ \”$(git rev-parse --abbrev-ref HEAD)\” != \”master\” ] || (git stash save — include-untracked pre-push && npm test) >&2"
},

“pre-push”: [
“pre-push”
],

--

--

Robert Herber
Kingstinct

Digital Nomad. Schedulist, Lyster, Kingstinct, Loco and PreCast.