Git hooks are scripts that run either before or after common Git tasks are performed. For example, a script could be configured to run before a project commit. Git Hooks are separate from other project files so in order to share them between projects, Kent introduces the ghooks Node module which makes sharing possible.
(electronic music) - Saving yourself from yourself. So, even though we already have linting set up and we have testing set up and that's awesome, sometimes you forget to run those things before you commit code and push it, right. And so, who here, raise your hand if you're familiar with Git Hooks. Okay, Git Hooks are a great thing. So basically, if you open up your Git directory, you'll find some really interesting things.
I actually recommend you spend like an hour someday just looking around in your dot GIT directory and your project. It's really kind of interesting. One thing that you'll find in there, we've got a couple people who are familiar with Git Hooks in the chat, actually one thing you won't find in there is a hooks directory. That's kind of funny. Normally, you'll have a folder called "hooks" when you initialize a Git repository. Does anyone have a hooks directory in their dot Git folder? Actually, you know what, you won't because the set up script removes them.
Most of the time, you'll just see a bunch of samples, so these sample files, but yeah there are hooks for commit message, that's the one that runs when you're about to add a commit message to a commit, post commit, post rewrite. So these are basically scripts that are run before or after something happens with Git. So before you rebase, before you push, before an update, all kinds of really interesting and cool things. So Git Hooks are really valuable, one trick about them though is that they're not committed to source control.
They're inside of that dot Git directory, so sharing Git Hooks has always kind of been a challenge. And so we're going to use a tool called G Hooks, which allows you to have shareable Git Hooks so that everybody who's working on your project can have scripts run, like some specific scripts run before they commit or push. And specifically for us, we want the scripts like the linting and the testing run before we commit anything. Does that make sense, like why we're about to do what we're going to do? Okay, there's a question? - [Audience Member] I was asking what editor they're using, but they aren't seeing the dot Git folder in their editor.
- Yeah, that's pretty common, for editors. - [Caller] Do you have to turn that on in Atom? - So yeah, if you're using atom, I'm pretty sure that atom shows it by default. Actually, no. I think there, if, you'll have to look in your setting somewhere in here, there's a way to torn that on, yeah, so lots of editors don't show dot files, so you'll have to VS Code I think too, you'll have to configure it to show dot files. But if you open up your terminal, and run LS-AL, like automatic, I'm not sure what you do in Windows, but you should see a Git there and you should be able to say, "CD dot git" and in here you'll find all the hooks directory and everything.
Note: This course was created by Frontend Masters. It was originally released on 08/09/2016. We're pleased to host this training in our library.
- Creating an open-source library
- Linting and testing
- Code coverage
- Installing and configuring Babel
- Peer dependencies
- Forking and renaming
- Continuous integration and automating releases