AZ-400: Designing and Implementing Microsoft DevOps Solutions

Branching Strategies for Source Code

Demystifying Git Hooks

Git Hooks are powerful scripts that execute at predefined points in your Git workflow. By automating checks, enforcing standards, and triggering tasks, hooks help maintain code quality and streamline development.

The image shows a list of Git hook sample files and a description explaining that Git hooks are customizable scripts that run at specific points in Git's workflow, acting like triggers.

Why Use Git Hooks?

Integrating Git Hooks into your workflow offers several advantages:

The image is a presentation slide titled "Git Hooks – Purpose and Power," highlighting two points: "Prevent problems" and "Enforce standards."

  • Prevent regressions by running linters or unit tests before committing code.
  • Enforce commit message conventions to maintain a readable history.
  • Automate builds, deployments, or notifications post-merge to accelerate delivery.

Note

Hooks reside in the .git/hooks/ directory. Rename or link your custom scripts (e.g., pre-commit) to enable them.

Client-Side vs. Server-Side Hooks

Client-side hooks run on a developer’s machine, catching issues early. Server-side hooks execute on the remote repository to enforce organization-wide policies.

The image compares client-side hooks, which operate on a local machine to catch issues early, with server-side hooks, which act on the server for broader control.

Common Client-Side Hooks

HookWhen It RunsPurpose
pre-commitBefore creating a commitRun linters, formatters, or unit tests
prepare-commit-msgBefore editing the commit messagePopulate templates or prepend metadata
commit-msgAfter editing the commit messageEnforce message style (e.g., Jira IDs, Conventional)
post-commitImmediately after a commitTrigger local notifications or analytics scripts
pre-pushBefore pushing to remoteRun integration tests or security scans

Common Server-Side Hooks

HookWhen It RunsPurpose
pre-receiveBefore any refs are updatedReject pushes that fail tests or violate policy
updatePer-ref check before updatingEnforce branch-specific rules
post-receiveAfter refs are updatedTrigger CI/CD pipelines, notifications, or deployment jobs

Warning

Server-side hooks require administrative access to the bare repository. Ensure you understand the impact before enabling these hooks.

Hook Examples in Action

Think of hooks as quality gates and automation triggers:

  • pre-commit: Blocks commits if linters fail
  • pre-push: Runs full test suites to verify stability before sharing
  • pre-receive: Stops forbidden changes from entering the central repo
  • post-merge: Refreshes dependencies or runs smoke tests after merging

The image explains Git hooks, describing them as tools for code quality control and defense against disruption, with examples like pre-commit, pre-push, pre-receive, and post-merge hooks.

Git Hook Execution Sequence

Understanding the order of hooks during commit and push helps you design effective scripts:

  • Client Side:
    1. pre-commit
    2. commit-msg
    3. post-commit
    4. pre-push
  • Server Side:
    1. pre-receive
    2. update
    3. post-receive

The image is a flowchart illustrating Git hooks, showing the sequence of events on the client and server sides during a commit and push process. It includes stages like pre-commit, commit-msg, post-commit, pre-receive, update, and post-receive.

Further Reading

Watch Video

Watch video content

Previous
GitHub Mobile Streamlining Pull Request Approvals on the Go