When you execute a Git commit, Git typically runs pre-commit hooks—scripts that inspect your changes before the commit is allowed. If these scripts find any issues, they can abort the commit. The --no-verify
option bypasses these hooks, letting you commit changes without the checks usually performed.
This guide delves into the usage, appropriate scenarios, and provides examples of when and how to use the --no-verify
option.
When to Use git commit --no-verify
Bypassing pre-commit hooks
Pre-commit hooks can perform various checks, like syntax linting, code formatting, or running tests. While these are generally helpful, there are times when you might need to bypass them:
- Urgency: Needing a quick fix or rollback where the hooks would cause unnecessary delay.
- Tool issues: Encountering temporary issues with the tools or scripts configured in the hooks.
- Partial commits: Committing work-in-progress to transfer or save state without triggering all checks.
Ignoring pre-push hooks
Similar to pre-commit hooks, pre-push hooks run checks before you can push commits to a remote repository. Using --no-verify
also bypasses these checks.
How to Use git commit --no-verify
Basic usage
To commit changes without running pre-commit hooks, use:
git commit --no-verify -m "Commit message"
This command commits the current staged changes to the repository with the provided commit message, skipping any hooks.
Examples
Here are a few practical examples demonstrating different scenarios where you might use git commit --no-verify
.
Example 1: Quick hotfix commit
Suppose you need to make a quick hotfix where running extensive tests is unnecessary:
git add hotfix_file.pygit commit --no-verify -m "fix: quick hotfix for urgent issue"
Example 2: Bypassing broken hooks
If you know that a pre-commit hook is broken or malfunctioning temporarily:
git add .git commit --no-verify -m "feat: add splash page fix"
Example 3: Work-in-progress commits
For work-in-progress commits that you do not want to be checked by hooks (especially when hooks are set to reject unfinished work):
git add .git commit --no-verify -m "WIP: adding new feature set, tests not yet passing"
Considerations and best practices
- Use sparingly: While
git commit --no-verify
is useful, it should be used sparingly and judiciously. Regularly skipping hooks can defeat the purpose of having quality checks in place. - Code quality: Ensure that the final commits, especially those merged into main development branches or deployed, undergo full checks to maintain code quality.
- Collaboration: When working in teams, communicate the reasons for bypassing hooks, especially if the commits will affect shared branches.
For further reading on git commit hooks, and how to bypass them, see the official Git documentation on Git hooks.