DevOps Bootcamp · Lesson

Pre-commit & Post-merge Hooks

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

Lesson 2 of 412 steps

Pre-commit & Post-merge Hooks is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

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!

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

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

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Pre-commit & Post-merge Hooks”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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