Git Hooks – Keep the Code Quality

I have worked with a small group on a project using GitHub repository. We all change the codebase 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 a 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,

  1. 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.
  2. Prevent commits through enforcing commit policy(seems wrong with commit requirements).
  3. Prevent pushes and merges that don’t match to defined standards or match some pre-defined expectations.
  4. 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,

  1. Remote Hooks (Serve-side hooks)— Scripts will run on the server hosting the Git repository
  2. 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,

  1. pre-commit — Fires before a git commit.
  2. prepare-commit-msg — Fires before the commit message prompt.
  3. 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.
  4. 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.
  5. 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.
  6. pre-rebase — Fires before git rebase changes anything.
  7. pre-receive– Fires every time somebody uses git push to push commits to the repository.
  8. update — Fires after pre-receive, and it works as the same way. It can call before anything is actually updated.
  9. post-receive —Fires after a successful push operation, making it a good place to perform notifications.

References –

Leave a Reply

Your email address will not be published. Required fields are marked *