Running composer install when you switch branch

When working with Composer and git branches, you will end up either:

  • reinstalling dependencies each time you switch branch
  • or meeting weird bugs because you didn't

because composer.json may have changed between branches.

To have composer automatically re-install dependencies when you switch to a branch, simply create a .git/hooks/post-checkout file in your project repository:

#!/bin/sh

cd $GIT_DIR/..

composer install

This is a git post-checkout hook (as the name suggest)