Git hooks are scripts that Git executes before or after events such as committing, merging, or pushing changes to a repository. These hooks are used to automate tasks or enforce policies within the development workflow, enhancing the consistency and quality of code submissions.
Git hooks were introduced as part of the Git version control system, which was first released in 2005 by Linus Torvalds to manage the development of the Linux kernel. Over time, the functionality of Git hooks has expanded, becoming a standard feature for developers to integrate custom workflows into their version control practices. Here are some key points in the evolution:
pre-commit
hook with Git 1.5.0, allowing for code validation before commit.post-commit
hook, which runs after a commit is created.pre-push
were introduced, enhancing the control over the push operation.There are two main categories of Git hooks:
pre-commit
- Runs before a commit is made, allowing for code validation.prepare-commit-msg
- Edits the commit message before it is shown to the user.commit-msg
- Checks the commit message entered by the user.post-commit
- Runs after the commit process, useful for notifications or logging.pre-push
- Executes before the push to a remote repository, allowing for last-minute checks.pre-receive
- Runs on the server before any references are updated.update
- Checks each reference being pushed separately.post-receive
- Executes after all references have been updated.
Git hooks are placed in the .git/hooks
directory of a repository. They are typically executable scripts written in languages like Shell, Python, or Ruby. Here's how to use them:
.git/hooks
directory.chmod +x scriptname
.