Skip to content

Instantly share code, notes, and snippets.

@carloseduardosx
Last active April 18, 2019 19:11
Show Gist options
  • Save carloseduardosx/9b42fbc220e3417e9c04f8bc2e07b5e5 to your computer and use it in GitHub Desktop.
Save carloseduardosx/9b42fbc220e3417e9c04f8bc2e07b5e5 to your computer and use it in GitHub Desktop.
Step by step to configure git hook

1. Create a folder called "git_template"

2. Create a folder inside git_template called "hooks"

3. Create the hook file "prepare-commit-msg" inside the hooks folders and then place the following code inside it:

#!/bin/bash
orig_msg_file="$1"
temp="temp_message"
branch=$( git branch | grep '^\*' | cut -b3- )
task=$( sed -E "s/feature\/|hotfix\/|react\///g" <<< "$branch" )

echo "${task}: " >> "${temp}"
cat "${orig_msg_file}" >> "${temp}"
echo "$(sed '/^\s*$/d' "${temp}")" > "${orig_msg_file}"
rm "${temp}"

Add the hook to git CLI configurations.

git config --global init.templatedir 'path_to_git_template_folder'

OBS: After this configuration every new git repository will include your hook, if you want already created repositories to also have this hook, then you need to reinitialize the repository, you can do this executing the following command inside the repository:

git init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment