Check out a free preview of the full Creating an Open Source JavaScript Library on Github course

The "Git Hooks" Lesson is part of the full, Creating an Open Source JavaScript Library on Github course featured in this preview video. Here's what you'd learn in this lesson:

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.


Transcript from the "Git Hooks" Lesson

>> [MUSIC]

>> Kent C. Dodds: 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?

>> Kent C. Dodds: 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 an hour some day just looking around in your .git directory in 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 kinda funny. Normally, you'll have a folder called hooks when you initialize a git repository. Does anybody have a hooks directory in their .git folder? Actually you know what? You won't because the set-up script removes them.

That's right. So when you don't run the set-up scripts you'll have a hooks directory in here. Actually, I'm gonna find another project to show you actually what that looks like.
>> Kent C. Dodds: So this is the JavaScript Air website project. And in here, you have that hooks directory and you have a bunch of script files.

This actually is using GHooks, which we're about to use, and so that's why it's got a bunch of content in here. 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 they're not committed to source control.

They're inside of that .git directory so sharing git hooks has always kinda been a challenge. And so we're going to use a tool called GHooks which allows you to have shareable git hooks so that everybody who's working on your project can have some specific scripts run before they commit or push.

And specifically throughout this we want the scripts like the linting and testing run before we commit anything. Does that make sense why we're about to do what we're gonna do? Okay, there's a question.
>> Speaker 2: I had a question, I was asking what editor are they using but they aren't seeing the .git folder in their editor.

>> Kent C. Dodds: Yeah, it's pretty common for editors.
>> Speaker 2: You think you could turn that on in Atom?
>> Kent C. Dodds: So yeah, if you're using Atom, I'm pretty sure that Atom shows it by default. Actually, no, you'll have to look in your settings somewhere in here. There's a way to turn that on, yeah, so lots of editors don't show dot files, and VSCode, yeah, I think, too.

You'll have to configure it to show.files. But if you open up your terminal and run ls-al, like on a Mac, I'm not sure what you do on Windows, but you should see a git there. And you should be able to say, cd.git and I'm sure you'll find all the hooks directory and everything.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now