34

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'?

user247702
  • 23,641
  • 15
  • 110
  • 157
alexbilbie
  • 1,177
  • 1
  • 11
  • 25

6 Answers6

7

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.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 6
    Using multiple repos means you get multiple histories for a single project. – Fred Foo Aug 13 '11 at 19:18
  • 3
    @larsmans: Not sure I understand your statement. All git repos represent the same history. If you're arguing against having multiple repos, you're essentially arguing against distributed version control. – Ryan Stewart Aug 13 '11 at 19:26
  • @Ryan Stewart - I agree with your approach. This is how I recently saw a demo at Uberconf. Worked really well and something I hope we can try with our CI server. – Tone Aug 14 '11 at 00:23
  • @Ryan: I think I misunderstood. I thought you suggested pushing only a selection of the commits to Blessed, but I guess that's not necessary. – Fred Foo Aug 14 '11 at 11:40
  • Could you explain why this is preferred over doing it with master, dev, and work-in-progress feature branches? – Hatshepsut Jan 22 '17 at 00:24
  • @Hatshepsut It's not preferred, this is just the way Ryan answered the OP's question. The question is about restricting people from committing to the master branch. If they aren't working with the same repo, there's no way for them to accidentally commit to master. If the build server is the only thing connecting to the Blessed repo on a regular basis, the chances are much smaller for an accidental master commit. It's not preferred, but there's an explanation on why the answer was posted. – sdouble Jan 26 '17 at 15:59
6

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.

thelem
  • 2,642
  • 1
  • 24
  • 34
4

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
1
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

Ahmed Younes
  • 964
  • 12
  • 17
1

Consider using a git access control layer like gitolite

Wissam Youssef
  • 800
  • 1
  • 10
  • 20
-7

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?

jsingh
  • 1,256
  • 13
  • 24