Git hook to block changes on certain folders

Viewed 615

I was wondering if there is any chance to create a hook for git to block changes on certain folders. For example there is a folder A with some files in it and the hook has to check if no files in this folder (or files in sub folders) have changed. If so an error should appear. How can I achieve this?

Thanks!

1 Answers

Below is a pre-commit hook that helps you not commit files you don't want to:

#!/bin/bash
git diff --cached --diff-filter=AM | grep -q gitblock
if [ $? -eq 0 ]
then 
  echo gitblock comment detected
  exit 1
fi

You should save it as .git/hooks/pre-commit in the repository you want to protect. After you make it executable, it will prevent you from committing any file that contains the text "gitblock" in a comment.

For more information: https://www.rdeeson.com/weblog/178/git-pre-commit-hook-to-block-accidental-commits

Related