Check if commit is first commit in pre-commit hook
Currently I’m using the pre-commit hook to disallow committing to the master branch (forces me to work on other branches and merge in changes). This doesn’t allow me to do an initial commit on a newly created repo. I want to add a check to allow a commit on master if it’s the first commit.
I’ve tried multiple versions of this with no luck…
if [[ `git shortlog | grep -E '^[ ]+\w+' | wc -l | tr -d ' '` == 0 -a `git symbolic-ref HEAD` == "refs/heads/master" ]] then echo "You cannot commit in master!" echo "Stash your changes and apply them to another branch" echo "git stash" echo "git checkout branch" echo "git stash apply" exit 1 fi
One Solution collect form web for “Check if commit is first commit in pre-commit hook”
git commit --no-verify ...
for the first commit. Then your pre-commit hook can apply simply to the master branch.