git - protect local master branch, prevent commit and push

If you've worked with a remote git hosting service like github, you're likely familiar with the concept of protecting a branch. Other remote git hosting services (vsts, bitbucket, gitlab, etc) also support the concept. It's a good idea to protect your remote in this way, but why stop there? You can also protect your local dev environment from human error by using git hooks.

A new git repo (`git init`) comes with various hook examples in the `.git/hooks/` directory. We can tie into two of those hooks, `pre-commit` and `pre-push`, to prevent commits to your local master branch, and to prevent attempting to push to the remote master branch (even from a local feature branch).

Place the two files found in the gist linked below in your repo's .git/hooks directory to prevent the two actions described above:

https://gist.github.com/aaronhoffman/ffbfd36928f9336be2436cffe39feaec



Hope this helps,
Aaron



Comments

Azure DevOps said…
This comment has been removed by a blog administrator.
Anu said…
This comment has been removed by a blog administrator.
un known said…
This comment has been removed by a blog administrator.

Popular posts from this blog

Search iPhone Text Messages with SQLite SQL Query

How to Turn Off Microsoft Arc Touch Mouse Scroll Sound Vibration

Configure SonarAnalyzer.CSharp with .editorconfig, no need for SonarCloud or SonarQube