Home > database >  Prevent a specific branch from being merged into ANYTHING?
Prevent a specific branch from being merged into ANYTHING?

Time:10-15

I've been looking into hooks, but I can't quite find exactly what I'm looking for. What I am trying to do is determine if it is possible to have an "un-mergable" branch - that is, a branch that we can perform experimental dev work on that is marked in such a way that it can never be merged ANYWHERE. I understand protected branches and can use that to protect things like Main from becoming corrupted... but let's say there's a new junior dev, and we want to give him his own branch playground, but make sure he doesn't merge his code into ANY other branches. Is that possible with vanilla Git?

CodePudding user response:

The short answer is just "no".

In the first place, Git doesn't care about branches. Git cares about commits. One can always run git merge hash and merge by hash ID. There is no branch name used here, so forbidding a merge of some given branch name would have no effect.

In the other first place, once I clone a repository to my laptop, my clone is mine. You cannot stop me from doing anything I want with that clone.

What you can stop me from doing is writing to your clone or clones. That's where you need to put this sort of thing. You probably should not try to do it by branch name, though, as this is ultimately doomed. You can offer new developers advice, and set up any "central receiving" repository to check for and handle common mistakes. Depending on who's hosting this "central receiving" repository, it can be easier or harder to catch particular errors. The most common method, which works really well in practice, is to make sure that nobody not already vetted can git push directly to the "source of truth" repository. Have them push to other repositories—"receiving depot" ones–where their code can be inspected first.

CodePudding user response:

As @torek points out, git itself doesn't care. But your repo server may have settings that give you the control you want. For example, here's the Branch Permissions config screen from BitBucket Cloud.

  • I can specify branches by type or name pattern.
  • Restrict write and merge access
  • You can see the rest.

BitBucket Cloud Branch Permissions

Locally, every user can merge branches any way they want. But I have restrictions on who can actually push branch changes to the remote server. No dev can directly merge and push to develop. But they can create Pull Requests, which go through code review. Those PRs can only be merged by certain people, so again, not every dev can just merge changes to develop.

BitBucket Cloud Branch Permissions Config

  •  Tags:  
  • git
  • Related