[资讯]2017重庆当代陶艺展于重庆金山意库启幕
Git hooks are scripts that run automatically before or after events like committing or pushing code, enabling automation such as linting, testing, and formatting. 1. Common hooks include pre-commit for linting, commit-msg for message validation, pre-push for tests, post-commit for logging, and pre-receive/post-receive for server-side tasks. 2. To set up a basic hook, create a script in the .git/hooks directory, make it executable, and optionally use tools like Husky for easier management. 3. Use cases include code formatting, commit message checks, local test runs, changelog syncs, and blocking accidental pushes. 4. Tips include avoiding slow hooks, ensuring proper exit codes, manual testing, and not relying solely on hooks for enforcement since they’re local by default.
Git hooks are scripts that Git runs automatically before or after certain events, like committing or pushing code. They let you customize Git’s behavior and add automation to your workflow — things like validating commits, running tests, or formatting code before changes get saved.
Local by default, powerful when used right
By default, Git hooks live in the .git/hooks
directory of your repository and are local to your machine. That means they don’t get shared when you push to a remote repo. But even so, they’re super useful for catching mistakes early and enforcing consistency in your own environment.
1. What kinds of Git hooks are there?
Git gives you a bunch of hook points. Here are the most commonly used ones:
-
pre-commit
: Runs before a commit is made. Good for checking code style or running linters. -
commit-msg
: Lets you validate or modify the commit message. -
pre-push
: Runs before you push changes to a remote repo. Useful for running tests or checking branch names. -
post-commit
: Runs after a commit is done — great for notifications or logging. -
pre-receive
/post-receive
: Run on the server side, often used in centralized workflows or CI pipelines.
Each hook is just a script file (like a shell or Node.js script) placed in the .git/hooks
folder with the right name and executable permissions.
2. How to set up a basic Git hook
Let’s say you want to check your code for linting errors before every commit. Here's how you'd do it:
- Navigate to your repo's
.git/hooks
directory. - Create or edit the
pre-commit
file. - Add a script that runs your linter — for example:
#!/bin/sh npm run lint
- Make sure the script is executable:
chmod x .git/hooks/pre-commit
Now, every time you try to make a commit, Git will run your linter first. If it fails, the commit won’t go through.
Note: If you're using a tool like Husky or Simple Git Hooks, they can help manage this setup more cleanly — especially across teams.
3. Common use cases for Git hooks
Here are some real-world scenarios where Git hooks come in handy:
- ? Code formatting: Automatically format your code before each commit using Prettier or Black.
- ? Commit message validation: Enforce conventional commit style by checking the message format.
- ? Run tests locally: Stop broken code from being committed by running unit tests in
pre-commit
. - ? Sync changelogs: Update version numbers or changelogs automatically on release branches.
- ? Prevent accidental pushes: Block pushes to certain branches like
main
unless you're in a CI environment.
These aren't replacements for CI checks, but they save time by catching issues earlier — right at the source.
4. Tips and gotchas when using Git hooks
Hooks are powerful, but here are a few things to keep in mind:
- They’re local by default, so if you want them shared across a team, you’ll need a tool like Husky or a setup script.
- Don’t write slow-running hooks — they’ll frustrate developers and might be skipped.
- Always make sure your scripts exit with the correct status code (
0
for success, non-zero to abort). - Test your hooks manually first — a broken hook can block commits or pushes unexpectedly.
Also, remember that not everyone on your team might have the same hooks set up. So while they’re great for developer convenience, don’t rely solely on them for enforcement in production.
That’s basically it. Git hooks are one of those tools that feel small until you realize how much smoother they make your workflow. Once you start using them, it’s hard to go back.
The above is the detailed content of What are Git hooks, and how can they be used?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Python development experience sharing: How to carry out version control and release management Introduction: In the Python development process, version control and release management are very important links. Through version control, we can easily track code changes, collaborate on development, resolve conflicts, etc.; and release management can help us organize the deployment, testing and release process of code to ensure the quality and stability of the code. This article will share some experiences and practices in Python development from two aspects: version control and release management. 1. Version control version control

Introduction to SVN SVN (Subversion) is a centralized version control system used to manage and maintain code bases. It allows multiple developers to collaborate on code development simultaneously and provides a complete record of historical modifications to the code. By using SVN, developers can: Ensure code stability and avoid code loss and damage. Track code modification history and easily roll back to previous versions. Collaborative development, multiple developers modify the code at the same time without conflict. Basic SVN Operations To use SVN, you need to install an SVN client, such as TortoiseSVN or SublimeMerge. Then you can follow these steps to perform basic operations: 1. Create the code base svnmkdirHttp://exampl

PHP code version control: There are two version control systems (VCS) commonly used in PHP development: Git: distributed VCS, where developers store copies of the code base locally to facilitate collaboration and offline work. Subversion: Centralized VCS, a unique copy of the code base is stored on a central server, providing more control. VCS helps teams track changes, collaborate and roll back to earlier versions.

Version Control: Basic version control is a software development practice that allows teams to track changes in the code base. It provides a central repository containing all historical versions of project files. This enables developers to easily rollback bugs, view differences between versions, and coordinate concurrent changes to the code base. Git: Distributed Version Control System Git is a distributed version control system (DVCS), which means that each developer's computer has a complete copy of the entire code base. This eliminates dependence on a central server and increases team flexibility and collaboration. Git allows developers to create and manage branches, track the history of a code base, and share changes with other developers. Git vs Version Control: Key Differences Distributed vs Set

How to perform version control and code management in Java development requires specific code examples. Summary: With the expansion of project scale and the need for team collaboration, version control and code management have become crucial aspects of Java development. This article will introduce the concept of version control, commonly used version control tools, and how to manage code. At the same time, specific code examples will also be provided to help readers better understand and practice. 1. The concept of version control Version control is a way of recording changes in file content so that specific versions of files can be accessed in the future.

How to implement version control and code collaboration in PHP development? With the rapid development of the Internet and the software industry, version control and code collaboration in software development have become increasingly important. Whether you are an independent developer or a team developing, you need an effective version control system to manage code changes and collaborate. In PHP development, there are several commonly used version control systems to choose from, such as Git and SVN. This article will introduce how to use these tools for version control and code collaboration in PHP development. The first step is to choose the one that suits you

1. Branching and merging Branches allow you to experiment with code changes without affecting the main branch. Use gitcheckout to create a new branch and use it when trying new features or fixing bugs. Once complete, use gitmerge to merge the changes back to the master branch. Sample code: gitcheckout-bnew-feature // Make changes on the new-feature branch gitcheckoutmain gitmergenew-feature2. Staging work Use gitadd to add the changes you want to track to the staging area. This allows you to selectively commit changes without committing all modifications. Sample code: gitaddMyFile.java3

Version Control in Collaborative Development Version control is a vital technology in software development that allows developers to track code changes, resolve conflicts, and collaborate on development. Version control is especially important in PHP continuous integration because it enables multiple developers to work on the same project at the same time without worrying about overwriting each other's changes. Choosing the Right Version Control System There are a variety of version control systems to choose from, the most popular include: Git: A distributed version control system that is highly scalable and feature-rich. Subversion (svn): A centralized version control system that is easy to use but has poor scalability. Mercurial: Another distributed version control system that is fast and lightweight. For most p
