Skip to content

Instantly share code, notes, and snippets.

@jamesrr39
Last active September 15, 2015 08:34
Show Gist options
  • Save jamesrr39/07c5eef65a9df8a2d77d to your computer and use it in GitHub Desktop.
Save jamesrr39/07c5eef65a9df8a2d77d to your computer and use it in GitHub Desktop.
pre-commit githook to make sure files are always added in master and only deleted in feature branches
#!/bin/bash
RED='\033[0;31m'
NC='\033[0m' # No Color
# pre-commit hook to check new files are always added to the master branch and deletions are always made in feature branches.
# useful when master should contain all the files ever added (eg. in a media project), and the feature branches can offer 'views' of parts of them
branch_name=$(git rev-parse --abbrev-ref HEAD)
non_added_files=$(git status -s | egrep -v ^A)
non_deleted_files=$(git status -s | egrep -v ^D)
if [ $branch_name == "master" ] && [ "$non_added_files" != "" ]; then
# only allow adding files to master
echo "on branch: $branch_name;"
printf "${RED}some changes are not additions, only allowing additions on master branch. Abort.${NC}\n"
echo "$non_added_files"
exit 1
fi
if [ $branch_name != "master" ] && [ "$non_deleted_files=" != "" ]; then
# only allow removing files from other branches
printf "${RED}some changes are not deletions, only allowing deletions on branch $branch_name. Abort.${NC}\n"
echo "$non_deleted_files"
exit 2
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment