patchhog.dev

How to Set Up Pre-Commit Code Security Scanning

June 21, 2026

How to Set Up Pre-Commit Code Security Scanning

The Core Problem: Why We Must Prevent Committing Vulnerable Code

In modern software development, security issues are often identified too late in the lifecycle. When a vulnerability is merged into a remote repository, it triggers a chain of expensive events: CI/CD pipelines run, security teams are alerted, and developers must interrupt their current tasks to write, test, and deploy a hotfix. In the worst-case scenario, sensitive data like API keys, database credentials, or private certificates are pushed to public repositories, requiring immediate rotation and potentially exposing the organization to security breaches.

To prevent committing vulnerable code, organizations must implement a shift-left security model. By moving security checks directly to the developer's local workstation, you catch issues at the earliest possible stage—before the code is ever committed to your local Git history. This zero-budget approach eliminates security debt before it accumulates, saving time, reducing CI/CD compute costs, and keeping your repository history clean.

How Git Hooks for Security Scanning Work

Git hooks are built-in scripts that Git executes locally when specific actions occur in your workflow. Git hooks for security scanning leverage these trigger points to analyze your code automatically before finalizing a commit or push.

The Git lifecycle includes several hook points, but the most relevant for security is the pre-commit hook. This hook runs immediately after you execute the git commit command, but before Git prompts you for a commit message or creates the commit object. If the pre-commit script exits with a non-zero status code, Git aborts the commit process, preventing the changes from being recorded.

While you can manually write shell scripts and place them inside your local .git/hooks directory, this manual approach does not scale across development teams. Git does not track the .git directory in version control, meaning your custom hooks cannot be easily shared, updated, or enforced. To solve this, developers use the pre-commit framework, a Python-based utility that acts as a package manager for Git hooks. This framework allows you to define your scanners in a single configuration file, which is checked into version control and easily shared across your entire engineering team.

Selecting the Right Local Static Application Security Testing (SAST) Tools

To build an effective local gate, you must choose the right local static application security testing sast tools. Not all security scanners are suited for local pre-commit execution. Enterprise-grade SAST platforms are often slow, resource-heavy, and require internet connectivity to communicate with cloud-based analyzers. Running these heavy tools locally would frustrate developers and slow down the commit process.

Instead, local scanning requires lightweight, fast, and deterministic tools that run entirely offline. The most effective tools for this task include:

For a comprehensive breakdown of how these lightweight local scanners compare to larger enterprise alternatives, read our analysis on Snyk Alternatives: Best Lightweight Security Scanners Compared.

Step-by-Step Guide to Setting Up Local Pre-Commit Security Scanning

Follow this step-by-step guide to install the pre-commit framework, configure your local static application security testing sast tools, and establish a local security gate on your workstation.

Step 1: Install the Pre-Commit Framework

To begin, you must install the pre-commit package manager on your local machine. Ensure you have Python installed, then run one of the following commands based on your operating system:

Verify that the installation was successful by checking the version in your terminal: pre-commit --version

Step 2: Create Your Configuration File

Navigate to the root directory of your Git repository and create a new file named .pre-commit-config.yaml. This YAML file will house the definitions for all the security scanners you want to run.

Step 3: Configure Secret Scanning with Gitleaks

Leaking credentials is one of the most common and damaging security mistakes in software engineering. To prevent this, add Gitleaks to your configuration file. For a detailed, step-by-step walkthrough on configuring secret scanning rules, refer to our guide on How to Scan Code for Secrets Before Pushing: A Step-by-Step Guide.

Add the following configuration to your file:

repos:

  - repo: https://github.com/gitleaks/gitleaks

    rev: v8.18.2

    hooks:

      - id: gitleaks

Step 4: Configure SAST Scanning with Semgrep

Next, integrate Semgrep to analyze your code structure and catch vulnerabilities like SQL injection, cross-site scripting, and insecure library usage. Append the Semgrep repository configuration to your file:

  - repo: https://github.com/returntocorp/semgrep

    rev: v1.65.0

    hooks:

      - id: semgrep

        args: ['--config=p/security-audit', '--error']

By passing the --error argument, you instruct Semgrep to return a non-zero exit code if it finds any high-severity security issues, which tells the pre-commit framework to block the commit.

Step 5: Register the Hooks with Git

With your configuration file saved, you must tell Git to execute these hooks during the pre-commit phase. Run the following command in your terminal:

pre-commit install

This command registers the pre-commit framework with your local Git repository. From now on, whenever you run git commit, Git will automatically execute the configured scanners against your staged files.

Step 6: Run a Baseline Scan

To ensure your existing codebase is clean before you begin committing new code, run a manual scan across all files in your repository:

pre-commit run --all-files

This manual scan allows you to identify and fix any existing security vulnerabilities or hardcoded secrets before they disrupt your daily development workflow.

Managing Developer Friction and False Positives

While local security gates are highly effective, they can introduce developer friction if they are too slow or produce excessive false positives. If developers feel that security tools are constantly blocking their work with irrelevant alerts, they will quickly find ways to bypass them.

To maintain high developer velocity while keeping your repository secure, apply these practical strategies:

Moving Beyond Local Gates: Repository-Level Automated Fixes

Local Git hooks are an essential first step, but they are not a complete security solution. They rely on developers manually installing the pre-commit framework on their individual machines, and they do not automatically fix the issues they detect.

To ensure comprehensive coverage and streamline your remediation workflow, you should combine your local pre-commit gates with a repository-level security platform.

This is where Patchhog fits into your development pipeline. Patchhog is an automated security tool that connects directly to your GitHub repositories via OAuth. It automatically scans every push for vulnerabilities using multiple deterministic scanners, covering SAST, Infrastructure as Code (IaC), secrets, malware, container risks, and dependency checks.

The primary advantage of Patchhog is its ability to automatically generate and provide paste-ready fixes for detected vulnerabilities. Instead of merely alerting you to a security issue and leaving you to figure out how to resolve it, Patchhog analyzes the finding and outputs the exact code change required to fix it. This drastically reduces the manual effort required to keep your code secure.

Patchhog integrates seamlessly with your existing GitHub repositories, enhancing them with continuous security scanning and automated fix generation. To see how Patchhog can streamline your security workflow, you can sign up for a 7-day trial, cancelable at any time.

Conclusion

Setting up pre-commit code security scanning is a powerful, zero-budget way to stop vulnerabilities from ever reaching your repository. By combining the local pre-commit framework with lightweight open-source SAST and secret scanning tools, you build a robust, immediate defense against common security mistakes.

To extend this protection to your entire GitHub organization and get automated, paste-ready fixes for every vulnerability detected in your code, explore Patchhog today.

FAQ

Can pre-commit hooks be bypassed by developers?

Yes, developers can bypass local hooks by appending the --no-verify flag to their git commit command. Because of this, local hooks should always be paired with centralized repository scanning to ensure all code is verified before merging.

How do local SAST tools affect git commit performance?

If configured correctly with lightweight, deterministic tools like Semgrep or Gitleaks, local scans typically take under three seconds. To keep performance high, configure hooks to only scan staged files rather than the entire repository.

How do I share my pre-commit configuration with my team?

By checking the .pre-commit-config.yaml file into your Git repository, any team member who clones the project can run pre-commit install to set up the exact same security gates on their local machine.

Related articles

How to Set Up Pre-Commit Hooks for Secrets DetectionJuly 7, 2026Aikido Alternatives: Best Developer Security Tools ComparedJune 27, 2026How to Scan Code for Secrets Before Pushing: A Step-by-Step GuideJune 20, 2026