Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git hook to block changes on certain folders

Tags:

git

hook

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!

like image 252
dblum Avatar asked Jul 01 '26 05:07

dblum


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

like image 62
LuVu Avatar answered Jul 04 '26 01:07

LuVu