EVER HEARD ABOUT GIT HOOKS?

Hello there! Today’s blog post is going to be about Git hooks. We have all worked with Git right? It is pretty amazing. For those who don’t know about git, Git is a version control system for tracking changes in computer files among multiple people. It is mainly used for source code management in software development. I got to know about Git when I was in my second year at my university. We had to integrate a system using java which was a group work. Thanks to git, we managed to develop our project without any problems.

For more information about Git, visit https://git-scm.com/doc.

Git hooks are scripts which trigger when you perform a specific action in Git. They are useful to automate tasks. There are two groups of these hooks. One is client side hooks, which are triggered by operations such as commiting and merging. Second one is server side hooks which runs on network operations such as recieving pushed commits.

Whenever you initialize a project in Git, you’ll get a .git folder in your working directory. Inside that, you’ll be able to find a folder called ‘hooks’. It contains a bunch of sample example scripts which are written as shell scripts, mostly in Perl. If you want to work your way with these hook scripts, you’re gonna have to rename them because all of them ends with .sample.

hooks.png

CREATING YOUR OWN HOOK

You can add your own sript to this folder as long as it is named correctly. Your script name should match to the hook name. The first line of your script needs to let Git know how to interpret the script.

Let’s try something out.

Create a file called pre-commit-msg inside the hooks folder without the .sample extension, and add the follwing to the file.

prtscrn.pngSince this is a script, we have to make it executable. To do that, you would run the follwoing command.

chmod.pngYou should now see this message replaced with the default message every time you run a git commit.

msg.png

That’s it! Likewise, you can customize each hook which are available inside the hooks directory.

Stay tuned for more cool stuff!

 

One thought on “EVER HEARD ABOUT GIT HOOKS?

Leave a comment