Skip to content

Instantly share code, notes, and snippets.

@joeddav
Last active November 13, 2024 20:30
Show Gist options
  • Save joeddav/fbc2c5bcb716ddb12daafe2c19d9cec6 to your computer and use it in GitHub Desktop.
Save joeddav/fbc2c5bcb716ddb12daafe2c19d9cec6 to your computer and use it in GitHub Desktop.
pre-push hook to ensure valid remote branch name
#!/bin/bash
# This hook ensures that only branches starting with a specific substring
# (set in valid_branch_start) can be pushed to.
#
# Should be placed in /path/to/repo/.git/hooks/pre-push
valid_branch_start="jdavison/"
# Read input about the branches being pushed
while read local_ref local_sha remote_ref remote_sha; do
# Extract the remote branch name
branch=$(echo "$remote_ref" | sed 's|refs/heads/||')
# Check if the remote branch name starts with required value
if [[ "$branch" != $valid_branch_start* ]]; then
echo "Error: You can only push to branches that start with '$valid_branch_start'."
exit 1
fi
done
# If all branches meet the requirement, allow the push
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment