prevent `git` from pushing commits with given commit message

How to prevent git from pushing commits that contain given string in commit massage, e.g. “DO NOT PUSH” ?

Context/usecase:

  • How to remove big files from old commits in bitbucket
  • git recover deleted file where no commit was made after the delete
  • Git - simplest way to sync a repository with a checked out branch
  • ZF2 private repository for project
  • Merging commits together in a branch
  • CruiseControl.NET hangs on git-submodule bad file descriptor with TFS
  • My typical workflow is:
    I hack hack, splitting work into micro commits,
    once things work I rewrite history, changing order of commit to group them reasonably and later squashing into bigger meaningful pieces.
    Once work is ready, things are ready to push!

    Now I would like git to prevent me from accidentally pushing into repository commits that are still in progress.
    I considered keeping “DO NOT PUSH” as part of commit message.
    How to make git automatically prevent me from pushing when it reaches such commit after git push?

    (On for pre-receive hook solutions: let’s consider github as example service, which AFAIK does not allow pre-recevie hooks, except in its “Enterprise” edition)

  • (Yet another) What's the best conversion from an SVN repository to HG repository(ies)?
  • How to maintain remote repo (Github, Bitbucket) up to date with the running server
  • How to ignore all files, but not sub-folders
  • Why does a seemingly possible merge using Git have conflicts?
  • gitosis + git error: fatal: '/home/git/repositories/idea-generator.git' does not appear to be a git repository
  • Git: How to recover stashed uncommitted changes
  • One Solution collect form web for “prevent `git` from pushing commits with given commit message”

    You can use a pre-push hook, but that remains a local hook which can be bypassed.
    See this pre-push example which does look at each commit message

    # Check for foo commit
            commit=`git rev-list -n 1 --grep '^foo' "$range"`
            if [ -n "$commit" ]
            then
    echo "ERROR: git pre-push hook found commit message starting with 'foo' in $local_ref"
    

    But the best way remains a pre-receive hook on the server side though. That way, the policy is enforced for all contributors. This is not always possible (when you don’t have direct access to the remote server like GitHub, BitBucket or GitLab.com)

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