Send Notifications when pushing a commit that modifies files inside a specific folder

Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.

Summary

Restrict users from committing to a specific folder or editing a file

Solution

On Bitbucket Cloud, we can apply restrictions to users only for specific branches using "Branch Restrictions" and not for folders/file.

Source: [https://support.atlassian.com/bitbucket-cloud/docs/use-branch-permissions/]

As a workaround, users can create a pre-commit file and add hooks to abort such commits

Reference: [https://stackoverflow.com/questions/58521962/git-hook-to-block-changes-on-certain-folders]

Steps to be followed 

  • Create a directory called ".gitHooks"

  • In this directory, put your hooks "Pre-commit" file with this name which is basically the shell script.

  • Access the repo directory and run this command 

    1 git config core.hooksPath  <path_to_.gitHooks>
  • Now if a user try to commit to a file(example:salesforce.sh), they will get following message "xxxxxxxxxxx WARNING DO NOT MAKE CHANGES TO THE FILE xxxxxxxxxxxxxxx"

  • In order to implement this across the team, repository members need to clone this repo OR pull the changes which consist of ".githooks" folder and then run the git config command as above 

Example:

1 2 3 % git add salesforce.sh  % git commit -m "salesforce.sh" xxxxxxxxxxx WARNING DO NOT MAKE CHANGES TO THE FILE xxxxxxxxxxxxxxx

Pre-commit hook-script

1 2 3 4 5 6 7 #!/bin/bash git diff --cached --diff-filter=AM | grep -q salesforce.sh if [ $? -eq 0 ] then    echo xxxxxxxxxxx WARNING DO NOT MAKE CHANGES TO THE FILE xxxxxxxxxxxxxxx   exit 1 fi

If you're getting the following warning instead of the above message, please provide permission to the file and then try to commit again

1 2 hint: The '.gitHooks/pre-commit' hook was ignored because it's not set as executable. hint: You can disable this warning with `git config advice.ignoredHook false`.
1 chmod +x .gitHooks/Pre-commit 

Updated on February 25, 2025

Still need help?

The Atlassian Community is here for you.