Prevent commit to specific git branches


I make the same mistake in a regular basis: start working in a feature, commit, push, and then realise I forgot to create my feature branch. This creates a little mess, esp. if current sprint branch is not set up as protected in GitLab (that's a mistake on its own, but it also happens).

I always commit changes from PhpStorm so I can use the nice “Commit Message” features, but I tend to push from command line. Apparently, PhpStorm doesn't have any feature to black list branches for commit and the “Protected branches” setting seemingly applies to push only (so I bypass it because I use a terminal).

What are my alternatives? I can't use pre-commit or commit-msg hooks because they're global to project, and I'm not proficient enough to know whether git provides some local trick.



Pre-commit hook is the only option asfaik. 

You can configure your own local githooks folder (copy hooks you need from your global project and make a new hooks folder and put it in .gitignore so you don't accidentally commit it.

Create the directory and use git config core.hooksPath nameOfDirectory

You can use the following hook: