Skip to content

nicholashughes/git-hooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Git Hooks

A collection of git hooks.

Overview

Git hooks are commands that 'hook' into git and run automatically when a specific git command is issued. These are some hooks that I like to use.

  • prepare-commit-msg: This hook is invoked by git commit right after preparing the default log message, and before the editor is started
  • post-checkout: This hook is invoked when a git checkout (or git clone) is run, after having updated the worktree

Activation

To activate these git-hooks, copy them into:
<YOUR_LOCAL_PROJECT_DIRECTORY>/.git/hooks

Be sure to enable the correct permissions (they must be executable)
chmod 755 <hook file>

Hooks

This a list of hooks that have been modified and what they do when activated.

prepare-commit-msg

This hook will prepend the branch name and branch description to every commit message. For example:
[Branch Name] <Commit message entered in the editor>

#!/bin/bash

# This way you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
  #BRANCHES_TO_SKIP=(master develop test)
  BRANCHES_TO_SKIP=()
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

Note: This code came from this gist

post-checkout

After a git checkout (or a git clone), this will delete all the .pyc and .pyo files found, starting from the repository root.

#!/bin/sh
#
# This hook is invoked when a 'git checkout' (or 'git clone') is run, after having updated the worktree
# Start from the repository root and do the following:
# delete all .pyc and .pyo files

cd ./$(git rev-parse --show-cdup)

# Delete .pyc and .pyo files
find . -name "*.pyc" -delete
find . -name "*.pyo" -delete
# Delete all empty directories
# find . -type d -empty -delete

About

A collection of Git hooks

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages