GithubHelp home page GithubHelp logo

hello-world's Introduction

Branching Protection Strategy

Purpose prevent merging directly to the master/main branch The idea is any branch can merge to develop branch and only develop branch can merge to master/main branch

Step1: Protect merging manually on the UI Solution: create a github workflow create .github/workflows/merge-master.yaml file add the the below content: on: pull_request: branches: - master jobs: merge_check: runs-on: ubuntu-latest steps: - name: Check if the pull request is mergeable to master run: | if [[ "$GITHUB_HEAD_REF" == 'develop' ]]; then exit 0; else exit 1; fi;

In addition to the above go to the repo settings, branches, add rule. set Branch name pattern to master/main select the following checkboxes only: Require a pull request before merging Require status checks to pass before merging Require branches to be up to date before merging search for merge_check and select it Include administrators

Save changes.

Step2: Protect merging using command line interface on the terminal

create .git/hooks/prepare-commit-msg file #!/usr/bin/env ruby

This git hook will prevent merging specific branches into master

Put this file in your local repo, in the .git/hooks folder

and make sure it is executable.

The name of the file must be "prepare-commit-msg" for Git to pick it up.

FORBIDDEN_BRANCHES = ["staging"]

def merge? ARGV[1] == "merge" end

def into_master? current_branch = git branch | grep '*' | sed 's/* //'.chop current_branch == "master" end

def merge_msg @msg ||= cat .git/MERGE_MSG end

def from_branch @from_branch = merge_msg.match(/Merge branch '(.*?)'/)[1] end

def from_forbidden_branch? FORBIDDEN_BRANCHES.include?(from_branch) end

if merge? && into_master? && from_forbidden_branch? out = git reset --merge puts puts " STOP THE PRESSES!" puts " You are trying to merge #{from_branch} into the master branch." puts " Surely you don't mean that?" puts puts " run the following command now to discard your working tree changes:" puts puts " git reset --merge" puts exit 1 end

#!/usr/bin/env ruby

This git hook will prevent merging specific branches into master

Put this file in your local repo, in the .git/hooks folder

and make sure it is executable.

The name of the file must be "prepare-commit-msg" for Git to pick it up.

ALLOWED_BRANCHES = ["develop"]

def merge? ARGV[1] == "merge" end

def into_master? current_branch = git branch | grep '*' | sed 's/* //'.chop current_branch == "master" end

def merge_msg @msg ||= cat .git/MERGE_MSG end

def from_branch @from_branch = merge_msg.match(/Merge branch '(.*?)'/)[1] end

def from_allowed_branch? ALLOWED_BRANCHES.include?(from_branch) end

if merge? && into_master? && !from_allowed_branch? out = git reset --merge puts puts " STOP THE PRESSES!" puts " You are trying to merge #{from_branch} into the master branch." puts " Surely you don't mean that?" puts puts " run the following command now to discard your working tree changes:" puts puts " git reset --merge" puts exit 1 end

References: https://github.community/t/improvement-suggestion-settings-branch-pattern-that-can-only-merge-to-target-branch-pattern/171870 https://bl.ocks.org/slattery/5eea0d6ca64687ecba6b https://stackoverflow.com/questions/46146491/prevent-pushing-to-master-on-github https://github.com/talenavi/husky-precommit-prepush-githooks

hello-world's People

Contributors

opeomotayo avatar valaxytech avatar yankils avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.