Setup git pre-push hook
Motivation
I often forget to run tests before pushing my commits. This is a bad practice.
Someone might use my broken code in a collaborative environment. I was looking
for a way to automatically run tests before creating a commit or pushing
commits.
The solution is Git Hooks: Customizing Git Hooks.
Git Hooks
Many hooks can be setup in a Git repository. For example, Git Hooks can run before creating a commit (pre-commit), after creating a commit (post-commit) and before pushing commits to a remote branch (pre-push). All of these are client-side hooks and client-side hooks are not
shared across local repositories. The other type of hooks are
server-side hooks that run after commits are pushed to a remote
server and policy is imposed on all users of the repository.
I have setup pre-push in a Git repository to avoid forgetting to run tests before sending updates to
the remote server.
Setup pre-push
The way to setup pre-push is as follows:
mkdir .githooks
# content of pre-push is in the code box below
touch .githooks/pre-push
# make sure pre-push is executable
chmod u+x .githooks/pre-push # set location of git hooks in git config git config core.hooksPath .githooks # test if pre-push runs as expected with dry run git push --dry-run origin master
Inside the pre-push script, I wrote a path to tests and run it.
#!/bin/bash path_to_test=<path_to_test> . $path_to_test
That's it! I won't forget to run tests anymore before sending my updates to a remote server.
A more comprehensive usage of Git Hooks is available in the page Customizing Git Hooks, but pre-push is sufficient for my use case.
Comments
Post a Comment