Git Hooks for Beginners
What are git-hooks?
Imagine a situation where you have to commit some code which will be strictly reviewed by your senior or you’re pushing your branch to an open source repository. You’re confident about your logic but you don’t want to mess up with minor issues such as unused variables or linting issues. You manage to push your branch just in the nick of time but forget to lint your code. Now what? You have to push another commit with those lint fixes. Do you reckon it would have been better if that second commit with lint fixes was avoided altogether? Well, that’s where git-hooks comes in.
According to Atlassian,
Git hooks are scripts that run automatically every time a particular event occurs in a Git repository
This definition is clear and concise in itself. You can imagine git-hooks as script files which attach themselves to git commands and get triggered automatically where you run specific git command. Git hooks can be set both for server and client. I’m focusing on the client hooks in this article.
Why use git-hooks?
Writing formatted and clean code is the telltale sign of an experienced developer. It’s not unusual to make mistakes, but when you do, having an automated system to alert you is handy. Git-hooks can be particularly useful to enforce naming conventions for commit messages, performing lint checks and fixes, displaying custom messages on events such as checkout etc.
Types of git-hooks
Every git repository has a folder called hooks
located inside the .git/
folder. If you navigate to it, you can see a bunch of files with extension .sample
. These are your hooks. Git creates them for each repository. These hooks are inactive by default, if you remove the extension they are activated. Let me give a brief explanation of the most frequently used hooks
How to use git-hooks
We can use any scripting language for writing git-hooks like Windows Batch
, Python
, Shell
,Ruby
etc. I’ll be using plain and simple Shell
here. Let me give you a low-down about scripts first before diving into git-hooks. If your scripts returns an exit value 0 then your git command will execute, anything other than 0 and it fails. This is how your git-hook will know if it needs to pass or fail your git command.
Let’s start by creating a simple hook which gets triggered when you’re trying to commit your code.
1.Navigate to your repository’s .git/hooks/
folder.
2. Locate the commit-msg.sample
file, rename it to commit-msg
. We have just activated our commit-msg
hook.
3. Sometimes you might have to make these executable, it works on Windows but you might need to do it for MAC or Linux. You can do that by the chmod
command in your terminal or git bash (won’t work on Window’s cmd)chmod +x .git/hooks/commit-msg
4. Open commit-msg
in your favorite text editor.
5. Replace everything with the following code and save your file.
6. We’re all set to test our hook. Make any change in your repo and try to commit. You’ll see the commit fails, it should look similar to what I have here
7. Let’s replace our hook with the following code
8. You can see that your commit passes now, it should look similar to below
You now know how to make git hooks work. Let’s try creating a meaningful hook to verify and lint our code when we try to commit. We will use Pinterest’s ktlint library to do so. Follow the steps below to integrate ktlint in your Android project.
1.In the root directory of your project, create a gradle file called ktlint.gradle
. You can set the name to anything you desire.
2. Copy the below content to ktlint.gradle
. Here we have added dependency for ktlint and added two gradle tasks. verifyFormatting
only checks and alerts you where the formatting issues are and applyFormatting
rectifies them.
3. Apply ktlint.gradle
file to your app/build.gradle
file as shown in line number 5 below
4. Edit your commit-msg
hook with the follow. We’ll be using ktlint’s suppport to verify our formatting
5. We’re all set with our hook. Test it out and you should see the following
See how handy that was! Git-hooks can be leveraged as a powerful tool to enforce quality code across your team. There are many other sample scripts you can find online, get creative!