Skip to content

Instantly share code, notes, and snippets.

@scarf005
Last active November 25, 2022 07:08
Show Gist options
  • Save scarf005/23e4232bbac5825c6ab693216c13ab90 to your computer and use it in GitHub Desktop.
Save scarf005/23e4232bbac5825c6ab693216c13ab90 to your computer and use it in GitHub Desktop.
forbid committing to default branch directly
#!/bin/sh
branch=$(git rev-parse --abbrev-ref HEAD)
default=$(basename $(git rev-parse --abbrev-ref origin/HEAD))
lang=$(locale | head -n 1 | cut -d'=' -f2 | cut -d'.' -f1)
if [ "$branch" != "$default" ]; then
exit 0
fi
case "$lang" in
ko_KR)
echo "기본 브랜치 $default에 직접 커밋할 수 없습니다."
;;
*)
echo "You can't commit directly to default branch $default."
;;
esac
exit 1

Forbid committing to default branch

Why?

  1. setup protected branch on main
  2. commit and push some stuff
  3. get rejected because you committed on main then tried to push it
  4. need a way to both block commit on local and remote default branch

How to install

  1. set hooks path
HOOKPATH=~/hooks
git config --global core.hooksPath $HOOKPATH
  1. copy content to re-commit hook
mkdir -p $HOOKDIR
(copy pre-commit content to $HOOKPATH/pre-commit)

See Also

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