Skip to content

Instantly share code, notes, and snippets.

@PandaWhisperer
Last active August 29, 2015 14:08
Show Gist options
  • Save PandaWhisperer/3f7c5764e5063db7c8f6 to your computer and use it in GitHub Desktop.
Save PandaWhisperer/3f7c5764e5063db7c8f6 to your computer and use it in GitHub Desktop.

Useful git hooks

You can teach an old git new tricks...

How to use

  1. Save each file with the same name as in the gist (for instance, pre-commit)
  2. Make it executable: chmod +x pre-commit
  3. Put it in the .git/hooks directory of a git project
#!/bin/bash
# git hook to show the last commit every time you switch branches
# Run `chmod +x post-checkout` to make it executable then put it into `.git/hooks/`.
set -e
prevHEAD=$1
newHEAD=$2
checkoutType=$3
[[ $checkoutType == 1 ]] && checkoutType='branch' || checkoutType='file' ;
if [[ $checkoutType == 'branch' ]]; then
echo 'Last commit: '`git log -n 1 --oneline --color $newHEAD`
fi
#!/bin/bash
# Based on https://gist.github.com/sindresorhus/7996717 by Sindre Sorhus
# git hook to notify if an important file has changed after `git pull`
# Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
notify_change() {
echo "$changed_files" | grep --quiet "$1" && echo "$1 has changed, you may need to run \`$2\`"
}
# Notify if package.json or bower.json has changed
notify_change package.json "npm install"
notify_change bower.json "bower install"
#!/bin/sh
# git hook to run `npm test` each time you commit
# If tests fail, the commit will be aborted (can be skipped using `--no-verify`)
# Run `chmod +x pre-commit` to make it executable then put it into `.git/hooks/`.
npm test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment