Composer post-checkout hook in git

One of the main advantages of developing with scriptable tooling is that they sometimes give you nice presents. While doing some research for a future article, I came across a composer post-checkout hook. The picture below displays very nicely how this works.

Composer post checkout hook

When you change branches there is a chance that you have different dependencies, and thus need to run composer install. This will warn you when thats the case.

Interested? See the gist below :).

Unfortunately the shell for hooks is not interactive, so you can’t ask the user whether he would like to do it right now. Still really useful, since the PHP ecosystem is evolving towards using composer.

A bit of background on post-checkout hooks

This little trick uses the post-checkout hook. This is what the official git documentation says about it.

After you run a successful git checkout, the post-checkout hook runs; you can use it to set up your working directory properly for your project environment. This may mean moving in large binary files that you don’t want source controlled, auto-generating documentation, or something along those lines.

There is more in the documentation.

One thought on “Composer post-checkout hook in git

  1. Thanks for sharing this gist! In case someone tries this on OS X, don’t forget to replace \e by 33 (the hex value of the escape character).

Leave a Reply

Your email address will not be published. Required fields are marked *