But, I would constantly spend countless time figuring out why the feature wasn't working until I realized I had once again forgotten to build the assets.
So, I started looking into git hooks to see what my options were.
What are git hooks?
Within git, there are 17 events that fire upon different actions being taken. During each event, there is a "hook" that anyone can hook scripts into.
Some of these hooks include:
- post-checkout: Ran after checking out a branch
post-merge: Ran after a
git pullis run
- pre-commit: Ran prior to a commit being created
- post-commit: Ran after the commit is created
Each git repo has a
directory that can contain scripts to be hooked into any of the hooks. You can drop a script that matches a hook's name into this folder.
Since this was a simple gulp command, I used a bash script. But, you could also use other languages, including Ruby or Python.
Hooking into post-checkout
In order to run code upon switching branches, we need the
hook. This hook fires after you checkout a branch or a file.
directory, I added a new file named
The post-checkout script gets called with three parameters:
- The previous HEAD
- The new HEAD
- A flag for if it's a branch that is being checked out. 1 for branch and 0 for not.
Since this also gets called when you check out a file, such as rolling back a file, this can cause a cycle where you roll back a built file and then it autogenerates the file again. To get around this, I check for the third parameter to make sure it's a
, I then added our gulp task, which builds all of our assets,
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Now that I have a rough idea of how git hooks work, I could see having scripts for validating commit messages, creating notifications, or automating other build tasks. I also came across articles on automating simple deployments using the post-receive hook , which I could see one used to build simple websites or even docker containers automatically.