Prevent direct commits on master branch in git rep

2020-05-24 19:36发布

My git repository has two branches, 'master' and 'dev'.

Code committed to 'dev' goes through an automated build process before it is tested. Code that passes this is then merged into the 'master' branch.

Is it possible, using hooks or something else, to prevent normal direct commits on the 'master' branch and only accept merges from 'dev' to 'master'?

标签: git merge commit
6条回答
地球回转人心会变
2楼-- · 2020-05-24 20:17

Not a direct answer: consider using repos instead of branches for this. Imagine three repos: local, dev, and blessed. Local = your own repo where you work. Dev = the repo you push all your commits to and the one that your build process is monitoring for changes. Blessed = the repo that only the build process can push to and which you pull from. Thus you commit into local and push changes to dev. Auto-build does all it's testing of the commits you pushed and on success, pushes them to blessed. Then you (or anyone else) can pick them up from blessed and continue work from there.

查看更多
Juvenile、少年°
3楼-- · 2020-05-24 20:22

If you're using GitHub, they have a feature to protect branches. Go to the GitHub settings for the repository, then branches and see the protected branches settings.

You can choose which branches you want to protect, and for each branch how you want to protect it. You can just prevent force pushes, require changes to be merged from another branch, or even require that your automated tests have passed.

See https://help.github.com/articles/defining-the-mergeability-of-pull-requests/

Bitbucket offer a similar feature.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-05-24 20:25

Consider using a git access control layer like gitolite

查看更多
▲ chillily
5楼-- · 2020-05-24 20:34

Make local branch

 command: git branch <branch name>

Go to branch through

Command: git checkout <branch name>     

Now your all local work save (through add . & commit ) into branch and then push to remote through

command : git push origin <branch name>

after that you can make pull request to master and merge to master. This is answer based on the linux (Ubuntu) system environment.

If any miss then let me know?

查看更多
Explosion°爆炸
6楼-- · 2020-05-24 20:35

You may want to use a commit-msg hook that checks whether the word merge occurs in the message for a tentative commit. Something like

grep -iq merge "$1" || exit 1

after a check for the branch. You may want to make the RE stricter than this. This is only a heuristic, of course, and anyone with write access to the central repo can circumvent this check.

查看更多
Fickle 薄情
7楼-- · 2020-05-24 20:42
npm install -D husky

npm i git-branch-is

configure it through package.json

    "husky": {
      "hooks": {
        "pre-commit": "git-branch-is -r \"^((?!master).)*$\""
    }
  }

enter image description here

查看更多
登录 后发表回答