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…

  • How to ignore files ending with ~ in git?
  • Git: break a large+messy feature branch into smaller branches
  • When force pushing to already sent pull request, ugly remnants of previous commits remains in issue
  • When should i create a new branch?
  • Github: error cloning my private repository
  • Find Git commits that contain multiple specific commits
  • if [[ `git shortlog | grep -E '^[ ]+\w+' | wc -l | tr -d ' '` == 0 -a `git symbolic-ref HEAD` == "refs/heads/master" ]]
        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

  • git: get effective GIT_DIR, if current dir is a working tree subdir and $GIT_DIR is unset
  • How do I release with BitBucket+Git+Maven
  • How to check out a specific tag with Egit?
  • How do you configure Jenkins to use git archive instead of git clone with Gerrit?
  • git svn - Can I use git and svn at the same time? no need interaction between git and svn
  • Git and Team Foundation Server
  • One Solution collect form web for “Check if commit is first commit in pre-commit hook”

    Just perform

    git commit --no-verify ...

    for the first commit. Then your pre-commit hook can apply simply to the master branch.

    Git Baby is a git and github fan, let's start git clone.