.git/hooks
Git hooks are categorized into
client-sideand
server-side hooks, each triggered at different stages of Git operations. Here's a list of all the available Git hooks:
These hooks run on the developer's machine during operations like commits, merges, and checkouts.
These hooks run on the Git server, typically during a push operation or when interacting with remote repositories.
When the commit command is executed:
git commit -m "test"
hook : Pre-commit
hook : prepare-commit-msg -
.git/COMMIT_EDITMSG
is created.Note: .git/COMMIT_EDITMSG
is file is not removed after the commit.
note : example of server side git hooks -> CI/CD pipelines run on a server (such as Jenkins, GitHub Actions, GitLab CI) after the code is pushed to a remote repository.
To install Husky: npm install --save-dev husky
npx husky-init && husky install
npm install --save-dev @commitlint/config-conventional @commitlint/cli
commitlint.config.js
in the root of your project with the following content:module.exports = { extends: ['@commitlint/config-conventional'] };
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'
npx: Executes the tool without installing it permanently.
--no-install: Prevents reinstalling a package that's already installed.
commitlint: Validates the commit message.
--edit "$1": Retrieves the commit message from the file generated by Git.
module.exports = { extends: ['@commitlint/config-conventional'], rules: { 'header-max-length': [2, 'always', 100], 'type-enum': [2, 'always', [ 'feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore', 'merge' ]], 'header-case': [2, 'always', 'sentence-case'], 'merge-message-format': [2, 'always', 'merged: <branch> into <target>'] }, };
To run tests before committing:
npx husky add .husky/pre-commit "npm test"