Skip to content

Latest commit

 

History

History
102 lines (72 loc) · 2.6 KB

File metadata and controls

102 lines (72 loc) · 2.6 KB
title How to customise your git commit message 🚀
published true
description Six steps to defining your git commit workflow
cover_image
tags git,bash

1. Add default template directory

git config --global init.templatedir '~/.git-templates'

Files and directories in the default template directory (~/.git-templates) whose name do not start with a dot will be copied to the specific $GitDir after it is created.

See git-init

2. Add default hook directory

mkdir -p ~/.git-templates/hooks

The hooks are stored in the hooks subdirectory of the default template directory.

See git-hooks

3. Add your first hook (e.g. prepare-commit-msg)

vi ~/.git-templates/hooks/prepare-commit-msg

Add a custom hook that puts your branch name to the top of your commit message. (Excluding master, develop)

#!/bin/sh

if [ -z "$BRANCHES_TO_SKIP" ]; then
  BRANCHES_TO_SKIP=(master develop test)
fi

BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"

BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)

if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then 
  sed -i.bak -e "1s/^/$BRANCH_NAME /" $1
fi

See git-hooks

4. Make the hook executable

chmod +x ~/.git-templates/hooks/*

Git hooks are not made executable by default.

5. Re-initialize git in each existing repository

git init

To use your new hook you have to re-initialize git.

See git-init

6. Commit 🚀

git commit -v

How does it look like?

alt text

7. Automation 🤖 If you'd like to add the same commit message for every git project you could configure something like this:

First create your template file. (For example ~/.gitmessage)

Why:
* 

After that you can simply add this file to your ~/.gitconfig

[commit]
    template = ~/.gitmessage

Hints 💡

👉 If a hook is already defined in your local git repository the new hook won't overwrite it. 👉 You might have to set the permissions on your new hook. (sudo chmod 775 .git/hooks/prepare-commit-msg)

RELATED:

Chris Beams about git commit messages