Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Hook to detect branch creation

Tags:

githooks

We need to write a client-side Git hook to detect a new branch creation from master. Whenever a new branch is created, a folder needs to be deleted from the branch.

I am not sure which hook is the best place to do this check or how to identify if the branch has just been created.

like image 896
Kashif Nazar Avatar asked May 07 '26 14:05

Kashif Nazar


1 Answers

A client hook is tricky, as:

  • it can be bypassed by a client, and
  • is not easily deployed on all clients.

A server hook is easier (update hook), since it receives a zero sha for new refs.
That same hook can list the content of a commit

git diff-tree --no-commit-id --name-only -r <SHA1>
# or
git ls-tree -d --name-only -r <SHA1>

If a specific folder is still there, it can reject the push with an helpful message.

like image 122
VonC Avatar answered May 11 '26 16:05

VonC