Automate Your Git Workflows with Git Hooks 🚀

Syed Muhammad Yaseen
CodeX

--

Helloooooooo!

Hope you’re doing great! This is SMY! 👋 Let’s Jump right in 🚀

I’ve settled up an example commit-msg hook that on every commit

↳ checks for branch name convention i.e [type]/ABC-[ticketnumber]-[objective]

↳ checks for commit-msg convention i.e [ABC-ticketnumber] [type]: objective

↳ checks for lints, prettier, and run unit tests

Gist Link: https://gist.github.com/smyaseen/17083d60d02a07b2a3122410e2d39b6f

Contents:

  • ⚡ `Wait What?`
  • ⚡ `But Why?`
  • ⚡ `But How?`

What -

↳ Git hooks are scripts that run automatically whenever a particular event occurs in a Git repository.

↳ They let you customize Git’s internal behaviour and trigger customizable actions at key points in the development life cycle.

↳ There are local hooks and server-side hooks.

↳ Local hooks run on your machine and server-side on the remote repository.

local hooks:

↳ pre-commit
↳ prepare-commit-msg
↳ commit-msg
↳ post-commit
↳ post-checkout
↳ pre-rebase

server hooks:

↳ pre-receive
↳ update
↳ post-receive

Why -

↳ For example, you want to run Lint checks before files are committed, so we use the pre-commit hook.

↳ another example you want to enforce the standard branch name and commit-msg convention, so we use the commit-msg hook.

↳ and lots more…

How -

↳ For easy setup, in a JavaScript project, install a library called Husky and follow the documentation

https://www.npmjs.com/package/husky

↳ An example is that I want to enforce branch names, commit message convention, and run lint, prettier, and tests.

↳ We can use multiple hooks. In my case, I will add a commit-msg hook. The reason I haven’t chosen pre-commit was that it doesn’t check for commit-msg as it runs before commit. I don’t want a developer to run checks and fail at commit message, fix, and run all again. I want to check for the commit message first.

↳ Create a commit-msg hook, following husky’s documentation, and paste the following example:

https://gist.github.com/smyaseen/17083d60d02a07b2a3122410e2d39b6f

↳ Now, whenever you commit, it will check for branch and commit naming convention along with running lint checks. Finally, it will run tests in the end, after everything else is good to go.

Result -

✨ Consistency.
✨ Standardization.
✨ Mind Off the Trivial.
✨ Automate & Focus on Impactful.

Wrapping Up:

We just Elevated Your Development Workflow with a Git Hooks. 🚀

…..

Now you can supercharge your development workflow 🚀

That’s it, folks! I hope it was a good read for you. Thank you! ✨

👉 Follow me

https://github.com/smyaseen

https://www.linkedin.com/in/sm-y

--

--

Syed Muhammad Yaseen
CodeX
0 Followers
Writer for

Full Stack Engineer ❯ JavaScript / TypeScript Ecosystem ❯ React, NextJS, NestJS, Microservices, AWS ❯ BS Computer Science ❯ Sharing Insights & Experiences