CodeX
Published in

CodeX

How to automate your work using Git’s add-on scripts?

Git Hooks Brief
  1. What is Git Hooks?
  2. Why most development teams are not adopting Hooks in their development process?

What is Git Hooks?

Why do we need Git Hooks?

Where can I find the Hooks configured on my repo?

What are all the available hooks which I can use?

Client Side Hooks

  1. prepare-commit-msg
  2. pre-commit
  3. applypatch-msg
  4. commit-msg
  5. fsmonitor-watchman
  6. pre-applypatch
  7. pre-merge-commit
  8. pre-push
  9. pre-rebase

Server Side Hooks

  1. pre-receive
  2. update
  3. post-update

How to configure a hook?

  • Path to the file that holds the commit message
  • Type of commit
  • Commit SHA-1, if this is an amended commit.
Example list of commits having my name ([Arunachalam]) at the beginning
Made prepare-commit-msg hook file executable
Change a file to test prepare-commit-msg hook
My name was prefilled as a template when trying to commit
After saving and exiting the commit message (COMMIT_EDITMSG) file
pre-push hook will fail due to returning a non-zero value
Failed to push due to non-zero exit code on pre-push hook
Commits pushed to the remote repo
error: failed to push some refs to 'git@github.com:arungogosoon/Git-Project.git'

Why most development teams are not adopting Hooks in their development process?

  1. Hooks are local to any given Git repository and are not synced with the remote repository. This means, if you try to clone again, you’ll not see your hooks there. So, it’ll be challenging to manage them across the development team. But, we can overcome this with 2 workarounds.

    1. Git allows us to move the hooks folder outside of .git folder. We can create a symlink (shortcut) from .git/hooks folder to the folder where we moved the hooks files. With this approach, we can track the changes in the hooks file and it’ll be synced with the entire team.
    2. By using some kind of alternative packages like husky . husky is a npm package which can be added innode project. husky helps us to configure commands to be executed on git hooks in the project configuration file. i.e., package.json file
  2. Though we find workarounds to sync the hooks file, the developer can easily skip the hooks validation by adding --no-verify with every git command. With this in mind, it’s best to think of Git hooks as a convenient developer tool rather than a strictly enforced development policy. Unless a server-side verification is set up, these cannot be verified.
  3. While git hooks are a compelling guardrail to help enforce quality, like any guardrail, they also come with some downsides including slowing down well-disciplined developers, and withholding opportunities for less-disciplined developers to gain that discipline. When a hook is configured to run test cases on every commit, it reduces developer productivity due to the long-running test cases. This happens especially with the compiling & transpiling languages (Eg. Java, Typescript, etc. ). Rather than focusing effort on guardrails to prevent developers from doing the “wrong thing”, focus on building discipline so that developers will learn to do the “right thing”.

Q & A

References

--

--

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store