Skip to content

Instantly share code, notes, and snippets.

@emailrhoads
Last active March 27, 2020 22:07
Show Gist options
  • Save emailrhoads/45465eca089e470f997366131cb24ec9 to your computer and use it in GitHub Desktop.
Save emailrhoads/45465eca089e470f997366131cb24ec9 to your computer and use it in GitHub Desktop.
[Set git hooks ] for when you dont have CI/CD for some reason #git

cd to your directory (optional)

cd .git/hooks

create your pre-push hook

touch pre-push

open in an editor

code pre-push

example code

#!/bin/bash
BRANCH=`git rev-parse --abbrev-ref HEAD`
PUSH_COMMAND=`ps -ocommand= -p $PPID`
DEV_BRANCHES="(dev|stg)"
PROTECTED_BRANCHES="^(master|dev|stg|release-*|patch-*)"
FORCE_PUSH="force|delete|-f"
LAST_COMMIT_MSG_WIP=`git log --pretty=format:"%s" -n 1 | grep -i 'WIP'`

if [[ "$BRANCH" =~ $DEV_BRANCHES ]]; then
	echo "do nothing for environment branches"
elif [[ LAST_COMMIT_MSG_WIP ]]; then
	echo "do nothing for WIP pushes"
else
	bundle exec rubocop --fail-level=autocorrect
	bundle exec rspec
fi

make sure the new script is executable

chmod +x .git/hooks/pre-push

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment