Module 10 Lesson 4: Git hooks: Automating your local environment
·DevOps

Module 10 Lesson 4: Git hooks: Automating your local environment

Let Git do the work for you. Learn how to use Git Hooks—local scripts that trigger automatically before a commit or push—to catch bugs, run tests, and format your code.

Module 10 Lesson 4: Git hooks: Automating your local environment

In high-quality engineering teams, we don't just "hope" the code is clean. We use automated tools. But sometimes you forget to run your tests before you commit.

Git Hooks are scripts that run automatically whenever a specific Git event occurs. They allow you to "Enforce" standards on your local machine.


1. Where do Hooks live?

Every Git repository has a folder called .git/hooks/. Inside, you will see several sample files ending in .sample.

  • pre-commit: Runs before you type your commit message. If this script fails (returns a non-zero exit code), Git will abort the commit.
  • commit-msg: Runs after you write your message. Used to enforce commit message standards.
  • pre-push: Runs before you send code to the server. Used to run heavy test suites.

2. Creating your first hook

To activate a hook, you create a script with the exact name (no extension) and make it executable.

Example: A Pre-commit Linter

  1. Create a file .git/hooks/pre-commit.
  2. Add a simple script:
#!/bin/sh
echo "Running automated checks..."
# Run your test command (e.g., npm test)
npm test
if [ $? -ne 0 ]; then
    echo "TESTS FAILED. Aborting commit."
    exit 1
fi
  1. Make it executable: chmod +x .git/hooks/pre-commit.

Now, if you try to git commit and your tests fail, Git will block the commit until you fix the code!


3. Sharing Hooks with your Team

The .git/hooks/ folder is not committed to your repository. This means your teammates won't get your hooks by default.

The Professional Solution: Husky

For many modern projects (especially JavaScript/TypeScript), we use a tool called Husky. It allows you to store your hooks in a regular directory (like .husky/) that is committed to the repository, and then automatically installs them in everyone's .git/ folder when they run npm install.

graph TD
    Commit["git commit"] --> Hook["Trigger: pre-commit script"]
    Hook --> Test["Run: npm test"]
    Test -- "Fail" --> Abort["Block Commit"]
    Test -- "Pass" --> Success["Commit Complete"]

4. Skipping Hooks

Sometimes you are in a rush and know the tests will fail, and you want to commit anyway.

git commit -m "Emergency fix" --no-verify

The --no-verify flag tells Git to ignore all hooks for that specific command. Use this sparingly!


Module 10 Practice Exercises

Let's automate and optimize your terminal:

  1. Alias Power: Create an alias graph that shows a compact, colorized version of your branch history.
  2. Archeology: Use git grep to find every instance of a specific keyword in your whole project.
  3. Recovery: Make a commit, note the ID, then reset --hard to a commit from 3 days ago. Use reflog to jump back to exactly where you were.
  4. Automation: Navigate to your .git/hooks folder. Read the pre-commit.sample file. Try to enable it by removing the .sample extension.

Checkpoint: If you successfully created a working hook or an alias, you are now working with the same setup as the top 1% of Git users!


Summary

In this lesson, we established:

  • Git Hooks automate checks and balances on your local machine.
  • pre-commit is the most common hook, used for linting and testing.
  • Hooks live in .git/hooks/ and must be executable.
  • --no-verify allows you to bypass hooks in an emergency.

Module Complete! You have the speed and the safety tools of a master.

Next Module: We’re moving into the real world. Welcome to Module 11: Real-World Projects, where we’ll apply everything we’ve learned to complex, hands-on scenarios.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn