Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
git hook to run a command after `git pull` and `git checkout` if a specified file was change for example, package.json or bower.json

Git hooks for NPM and Bower Projects

1. Copy theses files into your hooks folder

$ mkdir hooks && curl -o hooks/post-merge -O https://gist.githubusercontent.com/betorobson/23e5914b51e844bac5eaa6032d6f3f88/raw/e02ee803af1094a0dbff6c47febe02e1f5cb33b8/post-merge && curl -o hooks/post-checkout -O https://gist.githubusercontent.com/betorobson/23e5914b51e844bac5eaa6032d6f3f88/raw/e02ee803af1094a0dbff6c47febe02e1f5cb33b8/post-checkout

2. Add a script install at your package.json

...
  "scripts": {                                                       
    "install": "cp ./hooks/* .git/hooks/ && chmod -R a+X .git/hooks/"
  },                                                                 
...

3. Commit and push to remote repository

At this point your team will get any change on package.json and bower.json files

Thanks to these two guys:

#!/usr/bin/env bash
# fork from https://gist.github.com/jakemhiller/d342ad51505addf78ec628a16fd3280f
changed_files="$(git diff-tree -r --name-only --no-commit-id $1 $2)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
}
check_run package.json "npm prune && npm install"
check_run bower.json "bower prune && bower install"
exit 0;
#!/usr/bin/env bash
# MIT © Sindre Sorhus - sindresorhus.com
# fork from https://gist.github.com/sindresorhus/7996717
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
}
check_run package.json "npm prune && npm install"
check_run bower.json "bower prune && bower install"
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.