Skip to content

Git Hooks for Automation

Medium ~35 min

What you'll build

Three executable hooks in .git/hooks/ that enforce a commit message convention, block files over 1MB, and run tests before push.

Goals

  • Write a pre-commit hook as a shell script
  • Enforce a commit message format with commit-msg
  • Run tests automatically with pre-push
  • Make hooks executable and test them

Prerequisites

  • Basic shell scripting
  • Git commit and push workflow
  • File permissions (chmod +x)

Steps

  1. 1

    Write a pre-commit hook

    Create .git/hooks/pre-commit that checks for files larger than 1MB in the staged changes. If found, print an error and exit 1 (which blocks the commit).

    Code
    #!/bin/sh
    # .git/hooks/pre-commit
    max_size=1048576  # 1MB
    
    for file in $(git diff --cached --name-only); do
      if [ -f "$file" ]; then
        size=$(wc -c < "$file")
        if [ $size -gt $max_size ]; then
          echo "Error: $file is $(($size / 1024))KB, exceeds 1MB limit"
          exit 1
        fi
      fi
    done
    exit 0

    Expected result

    Committing a 2MB file fails with the error message; small files commit normally.

  2. 2

    Make the hook executable

    Git only runs hooks that are executable. Use chmod +x to set the permission, then test by committing a large file.

    Code
    chmod +x .git/hooks/pre-commit
    # Test:
    dd if=/dev/zero of=bigfile.bin bs=1M count=2
    git add bigfile.bin
    git commit -m 'Add big file'  # Should be blocked

    Expected result

    The commit is blocked with the size error.

  3. 3

    Write a commit-msg hook

    Create .git/hooks/commit-msg that enforces a 'type: message' format (e.g. 'feat: add login'). Read the message from $1, check with grep, exit 1 if invalid.

  4. 4

    Write a pre-push hook

    Create .git/hooks/pre-push that runs your test suite. If tests fail, the push is blocked. Read stdin for ref updates (you can ignore it for simple hooks).

  5. 5

    Test all hooks end-to-end

    Test each hook: try a bad commit message (blocked), a large file (blocked), and a failing test (push blocked). Then verify valid commits and pushes succeed.

    Code
    # Test commit-msg:
    git commit -m 'bad message'  # blocked
    git commit -m 'feat: valid message'  # succeeds
    
    # Test pre-push:
    # (with a failing test in the suite)
    git push  # blocked if tests fail

    Expected result

    Invalid commits and pushes are blocked; valid ones succeed.

Try it yourself

What you learned

You automated quality checks with Git hooks — pre-commit, commit-msg, and pre-push are the standard way to enforce conventions before code is shared.

Related Resources

Related Lessons

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic