0Pricing
DevOps Bootcamp · Lesson

Pre-commit & Post-merge Hooks

Implement practical examples of pre-commit hooks for linting and post-merge hooks for notifications.

Pre-commit & Post-merge Hooks is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Practical Git Hooks Introduction

In the previous lesson, we learned about Git hooks and their different types. Now, let's get practical!

We'll dive into implementing two powerful hooks: pre-commit and post-merge. These allow you to automate checks before a commit and actions after a merge.

Pre-commit: Your Quality Gate

The pre-commit hook runs before a commit operation, but after you've used git add to stage your changes.

  • It's perfect for enforcing code quality and style.
  • You can use it to run linters, style checkers, or even basic unit tests.
  • If the script exits with a non-zero status, the commit is automatically aborted.

This ensures only high-quality, compliant code enters your repository.

Setting Up Pre-commit Hook

To create a pre-commit hook, you need to create an executable file named pre-commit inside your project's .git/hooks/ directory.

Git provides example hooks with .sample extensions. You'll remove that extension and add your custom script.

Remember to make the file executable using: chmod +x .git/hooks/pre-commit

Example: Prevent 'TODO' Commits

Let's create a simple pre-commit hook that prevents you from committing files that still contain the word "TODO". This is a common practice to ensure no unfinished tasks slip into your codebase.

The script will check all staged files for this keyword. If found, it will stop the commit.

Pre-commit Hook: No TODOs!

Here's a simple shell script for your .git/hooks/pre-commit file. It checks staged .txt files for the word "TODO".

Steps:

  1. Create .git/hooks/pre-commit
  2. Paste this code
  3. Run chmod +x .git/hooks/pre-commit
#!/bin/sh

# Check staged .txt files for "TODO"
for file in $(git diff --cached --name-only --diff-filter=ACM | grep '\.txt$'); do
  if grep -q "TODO" "$file"; then
    echo "ERROR: Found 'TODO' in staged file: $file"
    echo "Please remove TODOs before committing."
    exit 1
  fi
done

exit 0

Testing Your Pre-commit Hook

To test this hook, first create a file (e.g., test.txt) and add "TODO: Finish this" to it. Then, try to commit it:

git add test.txt
git commit -m "Add test file"

The commit should be aborted, and you'll see the error message from your hook. Remove "TODO" from test.txt, and the commit will succeed!

Post-merge: Automate After Merges

The post-merge hook runs automatically after a git merge command successfully completes.

  • It's useful for integration tasks that should happen after code is combined.
  • Examples include notifying team members, updating project dependencies (e.g., npm install), or running a build script.
  • Unlike pre-commit, this hook doesn't affect the merge outcome.

It's a great way to automate post-merge processes.

Setting Up Post-merge Hook

Similar to pre-commit, you create a file named post-merge inside your .git/hooks/ directory.

Again, ensure it's an executable file:

chmod +x .git/hooks/post-merge

This script will then run every time a merge completes in your repository.

Example: Merge Completion Alert

Let's create a post-merge hook that simply prints a message to the console, confirming a merge just happened. In a real-world scenario, this could trigger a CI/CD pipeline or send a notification to a chat application.

Post-merge Hook: Notification

Here's a simple shell script for your .git/hooks/post-merge file. It prints a confirmation message.

Steps:

  1. Create .git/hooks/post-merge
  2. Paste this code
  3. Run chmod +x .git/hooks/post-merge
#!/bin/sh

# This hook runs after a merge operation.
echo "--- Post-merge hook triggered ---"
echo "A merge has just completed in your repository."
echo "You might want to run 'npm install' or 'bundle install' now."
echo "---------------------------------"

exit 0

Hook Check: Pre-commit vs. Post-merge

Which of the following tasks would typically be handled by a pre-commit hook?

Recap: Hooks in Action

Great job! In this lesson, you've learned to implement practical Git hooks.

  • Pre-commit hooks act as a quality gate, running checks like linting or testing *before* a commit.
  • Post-merge hooks automate actions *after* a merge, such as notifications or dependency updates.

These hooks are powerful tools for enforcing standards and automating workflows in your Git repository. Continue exploring other hook types to further customize your Git experience!

Frequently asked questions

Is the “Pre-commit & Post-merge Hooks” lesson free?

Yes — the full text of “Pre-commit & Post-merge Hooks” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Pre-commit & Post-merge Hooks”?

Implement practical examples of pre-commit hooks for linting and post-merge hooks for notifications. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Pre-commit & Post-merge Hooks” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding Git Hooks
  2. Pre-commit & Post-merge Hooks
  3. Customizing Git Configuration
  4. Sharing Hooks with Husky
← Back to DevOps Bootcamp