Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Created September 6, 2013 15:46
Show Gist options
  • Save sindresorhus/6465739 to your computer and use it in GitHub Desktop.
Save sindresorhus/6465739 to your computer and use it in GitHub Desktop.
Git hook to install npm dependencies after a `git pull`. Run `chmod +x post-merge` and put it in `.git/hooks/`. Though could really do whatever.
#!/bin/sh
npm install
@GianlucaGuarini
Copy link

Your example is really easy, but I need something a bit more complicated:
how is it possible to trigger an npm install or bower install on a post merge ONLY if my local package.json or bower.json gets updated?

Working in big team, anytime someone installs a new grunt or bower dependency he must communicate that by voice or chat to all the other developers so it would be great to trigger it just by git pull if necessary

Another idea could be also to trigger any npm or bower command by checking the commit messages example:

added the new jquery slider carousel :: bower install jquery

what's your opinion?

@sindresorhus
Copy link
Author

You could save the MD5 hashsum of package.json and check if it changes between merges, but that's really just overcomplicating it. Can't you just always run npm install & bower install after every merge? If nothing has changd they won't do anything.

@GianlucaGuarini
Copy link

I think running npm install & bower install on any git pull is not really an elegant solution for three reasons:

  • this really means that pulling a project every 10/15 minutes ( like often we do ) it will trigger many other useless npm and bower commands that could take at least ~30seconds per each ( imagine how many seconds wasted )
  • npm install and bower install do not delete the old packages already fetched
  • this solution does not work if you then add the composer commands ( we work mostly on Laravel projects )

@sindresorhus
Copy link
Author

I created a quick script for you. Let me know how it works: https://gist.github.com/sindresorhus/7996717

@WebReflection
Copy link

+1 to latest script

@pajtai
Copy link

pajtai commented Dec 20, 2017

this doesn't work for me with zsh and nvm - can't find npm

@simonhaenisch
Copy link

simonhaenisch commented Dec 23, 2017

@pajtai see nvm-sh/nvm#688 (comment) (googled for "git hook nvm path")... don't be so lazy and do some research 🤓 your problem is not related to this script.

@robrecord
Copy link

robrecord commented Jul 6, 2019

#!/usr/bin/env bash

set -e

prevHEAD=$1
newHEAD=$2
checkoutType=$3

[[ $checkoutType == 1 ]] && checkoutType='branch' ||
checkoutType='file' ;

check_run() {
    echo "$changed_files" | grep --quiet "$1" && eval "$2"
}

[[ $checkoutType = 'branch' ]] && {
    
    changed_files="$(git diff-tree -r --name-only --no-commit-id $prevHEAD $newHEAD)"
    
    # node - use whichever works for you
    check_run yarn.lock "yarn install"
    # check_run package-lock.json "npm install"

   # composer
    check_run composer.lock "composer install"
}

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