Skip to content

Instantly share code, notes, and snippets.

@bpgould
Last active February 13, 2023 02:19
Show Gist options
  • Save bpgould/e11356f42a2fb42ecda41540742e362c to your computer and use it in GitHub Desktop.
Save bpgould/e11356f42a2fb42ecda41540742e362c to your computer and use it in GitHub Desktop.
block commit to main using git hooks
#!/bin/bash
protected_branch='main'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [[ "$protected_branch" = "$current_branch" ]]; then
input=''
read -p "You're about to commit to master, is that what you intended? [y|n] " -n 1 -r </dev/tty
if [[ "$input" == [yY] || "$input" == [yY][eE][sS] ]]; then
exit 0 # commit will execute
fi
exit 1 # commit will not execute
else
exit 0 # commit will execute
fi
@bpgould
Copy link
Author

bpgould commented Feb 12, 2023

just add this to a file named .git/hooks/post-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment