Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 17, 2020 02:59 pm GMT

Use Git Hooks to ease your development workflow

Git hooks allow us to run custom scripts whenever certain important events occur in the git repository such as commit, push, merge. Basically git hooks are stored in the .git/hook directory of your local repository.

Initially all hook files contain .sample as extension, which indicate those are disabled. These sample hooks are stored in one of the below locations as per your operating system,

  • Linux: /usr/share/git-core/templates

  • Windows: C:\Program Files\Git\mingw64\share\git-core\templates

  • Mac OS: /usr/local/git/libexec/git-core

Whenever you initialize a local git repository these sample hooks templates which are already stored on your machine get copied into your local repository.

All hooks pre-fix with pre label will execute before the particular event happens and hooks with pre-fix post will execute after the event.

For Example: pre-push hook will execute before the push operation.

As hooks are disabled by default, we need to perform below steps to enable them ,

  • Remove .sample extension from the hook name.
  • Make that hook file executable using command chmod +x

Before we start remember this:

  • As we see here hooks present inside .git/hook/ directory which means these are not source control. You can use symbolic link.
  • You can bypass pre-commit git hook locally by adding --no-verify flag.

Lets start with the practical use of git hooks.

Scenario 1: In one of the projects we used to create a feature-branch from the environment branch(master,dev,release). We should not commit our code directly on the environment branches.

Solution: pre-commit hook is run first, so we will use this hook. copy below code snippet in your pre-commit hook.

#!/bin/sh# Avoid doing commit in environment branches.BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)forbidden_branchs="^(master|dev|release)$"message="You should not commit directly into ${BRANCH_NAME} branch.\nPlease create a new branch from '$BRANCH_NAME' and Try again."if [[ $BRANCH_NAME =~ $forbidden_branchs ]]then    echo -e $message    exit 1fi

In windows operating system instead of #!bin/sh use #!C:\Program Files\Git\bin\sh.exe path where git installed its shell script.

Now git provides you the freedom to select scripting language. If you are comfortable with other scripting languages like python, ruby, powershell you just need to add the path of its library on the first line of the hook.

Always remember, If your hooks returns zero means that it is succeed. If it exists with any other number it is failed. In the above code snippet we exit code 1 which means hook failed so commit will not happen.

With git hooks imagination is the limit! I have mention two more practical usage of git hook in my LinkedIn article. Please refer to my original post here.

Let me know if the article is useful.
Happy coding!


Original Link: https://dev.to/rajh24/use-git-hooks-to-ease-your-development-workflow-2ijh

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To