Skip to content

tobiasbueschel/git-is-branch-protected-cli

Repository files navigation

git-is-branch-protected-cli

Build Status version downloads codecov

semantic-release MIT License

CLI to check whether current Git branch is protected

Install

npm install --global git-is-branch-protected-cli

Usage

gibp --help

  Usage
    gibp [options]

  Options
    --protected-branches, -b Specify protected branches [Default: master, main, develop]
    --silent, -s Do not show any error messages

  Examples
    gibp -b "master, main, develop, another-important-branch"
    gibp -b master

Why use this tool?

It's easy to forget setting up branch protection on GitHub, GitLab or any other Git hosting service, and before you know it, you've pushed to master during your late night coding session. Or perhaps you have scripted automatic updates that will push and create pull requests to several repositories at once and want to ensure you're not pushing to the wrong branch?

This tool comes to the rescue and with the help of Git Hooks and husky prevents you from accidentally pushing to your remote master, main or develop branch. All you need to do is install husky and git-is-branch-protected-cli to your project:

npm install husky git-is-branch-protected-cli --save-dev

and add the following to your package.json to protect anyone from commiting to master, main, or develop:

{
  "husky": {
    "hooks": {
      "pre-push": "gibp"
    }
  }
}

you can also instruct gipb to project other branches using the following configuration as an example:

{
  "husky": {
    "hooks": {
      "pre-push": "gibp -b \"master, main, develop, foo-bar\""
    }
  }
}

After this setup, the pre-push hook will fail if you are on a protected branch as gibp returns exit code 1, thereby preventing you from accidentally pushing to the wrong remote branch.

⚠️ This is not a bulletproof solution as someone could still write to the branch using --no-verify. Therefore, please also consider setting up server-side branch protection.

Related

License

MIT © Tobias Büschel

About

🔒 CLI to check whether the current Git branch is protected (i.e. is main or develop)

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project