Git Hooks — Keep the Code Quality
I have worked with a small group for a project using GitHub repository. We all change the code base due to our development stage. But some others in my group haven’t perfect coding skills. I was very upset while re-correcting all the codes after their commits. In this case, I got the great offer from Git Hooks, Let’s see what is Git Hooks.
Git hooks are pre-defined simple scripts which can run automatically every time particular actions(before or after a relevant git event or action is triggered) occurs in a GitHub repository. Git Hooks can be written in any language.
The common benefits of Git Hooks are,
- It is very useful for a variety of tasks, For example, you can test some syntax conditions on files being committed, or you can run some tests cases on files being committed.
- Prevent commits through enforcing commit policy(seems wrong with commit requirements).
- Prevent pushes and merges that don’t match to defined standards or match some pre-defined expectations.
- Can work for continuous deployment.
When the actions are occurring, Git will run these predefined executable Git hooks script and Git will allow the action (commit/merge/push) to occur as long as the Git hook exits with no errors (status want to be 0 — no errors).
There are two types of Git hooks,
- Remote Hooks (Serve-side hooks)— Scripts will run on the server hosting the Git repository
- Local Hooks (Client-side hooks)—Scripts will run on the developer’s system
You can think of a Git hook as an event or action that gets triggered before or after different stages of the control process. Some hooks event or actions are,
- pre-commit — Fires before a git commit.
- prepare-commit-msg — Fires before the commit message prompt.
- commit-msg — Fires to populate the text editor with a commit message. This is much like the prepare-commit-msg hook, but it’s called after the user enters a commit message.
- post-commit — Fires after a git commit. This is called immediately after the commit-msg hook. anyway, It can’t change the outcome of the git commit operation.
- post-checkout — Fires after changing branches. It works like the post-commit hook, but it’s called whenever you successfully check out a reference with git checkout.
- pre-rebase — Fires before git rebase changes anything.
- pre-receive– Fires every time somebody uses git push to push commits to the repository.
- update — Fires after pre-receive, and it works as the same way. It can call before anything is actually updated.
- post-receive —Fires after a successful push operation, making it a good place to perform notifications.