3

This might be just a case of me misunderstanding git, but my situation is as follows:

I have a master branch where I want to have only stable working code. Any addition to this branch must come from a pull-request in bitbucket (could be github or something else, it's just the tool we use). So I want to create a git hook in my local repository to prevent any merging into the master branch but that also allows me to fetch/pull changes from my remote repository at bitbucket.

I already have git configured to disallow fast-forward merges.

I looked into these solutions that provide something similar, that is to say, a whitelist and a blacklist of branches that can/can't merge into master:

Git hook prevent merging specific branch

https://bl.ocks.org/slattery/5eea0d6ca64687ecba6b

Also this solution prevents me from commiting directly into master, which is also very useful:

Git: Prevent commits in master branch

However I'm not clear if any of these would prevent me from pulling in changes made by pull-requests in bitbucket/github into my local working repository.

Many thanks!

[edit] As suggested below I had also already setup branch permissions in my bitbucket repo, but that prevents me only from pushing merged master branches. I want to prevent merging into master locally instead.

3 Answers3

1

So I want to create a git hook in my local repository to prevent any merging into the master branch (...).

What are you protecting your local master branch from? Yourself?

Anyway, the solution is to setup the bitbucket repo so that you cannot push your changes to master. This means that even if you screw up things locally then you won't be able to push the changes to remote, they will have to be done via a pull request.

Bitbucket branch permissions are explained at Using branch permissions.

tymtam
  • 31,798
  • 8
  • 86
  • 126
  • I'm actually trying to protect the respective local `master` branches of everybody on the team from themselves, but yes, pretty much. I have already set up branch permissions on bitbucket, and that is indeed pretty helpful, but it doesn't prevent me from crewing things up locally, which is what I'm trying to achieve. The team isn't experienced with git, (full disclosure, neither am I) and I'd like to avoid having them revert changes to their local repo or reset local branches to a remote. – chicobaptista Oct 03 '18 at 12:16
1

I'm not clear if any of these would prevent me from pulling in changes made by pull-requests in bitbucket/github into my local working repository.

No, as long as:

  • you pulling from another branch than master
  • or you do pull new commits from origin/master to your local master, but detect that the "from" branch is an origin/xxx branch (in which case, you allow the commit to proceed)

The problem with any client-side hooks is that you then need to deploy/distribute that hook to all your team members.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

I like the way you think, however I think that you should try to protect your remote master and then have your developers create their code in respective topic branches, not in their local master branches.

Try and look at these usage of git : microsoft git usage

serup
  • 3,676
  • 2
  • 30
  • 34