Skip to content

Instantly share code, notes, and snippets.

@flesler
Forked from betorobson/post-checkout
Created April 19, 2017 14:02
Show Gist options
  • Save flesler/a2f5d1286fbb8fd467dc25824d564485 to your computer and use it in GitHub Desktop.
Save flesler/a2f5d1286fbb8fd467dc25824d564485 to your computer and use it in GitHub Desktop.
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"
#!/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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment