Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict merge from specific branches

I would like only specific branches merge to specific branches.

Some example: I want to do master branch accept pull requests only from test branch, and test branch accept only from development branch.

How can I do this restriction on TFS/Git?

We have some policies about reviewers and build validation.

like image 217
Toygar Avatar asked May 24 '19 08:05

Toygar


People also ask

How do I restrict git merge?

No, GitHub doesn't let you restrict who can perform a merge. However, if you want to require a specific group of people to approve a PR before merging, use the CODEOWNERS file and require an approval from a code owner before merging in the branch protection settings.

How do I stop a branch from merging?

Use git-reset or git merge --abort to cancel a merge that had conflicts. Please note that all the changes will be reset, and this operation cannot be reverted, so make sure to commit or git-stash all your changes before you start a merge.

How do I merge specific branches?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.


Video Answer


2 Answers

If you have branch policies you can do a workaround to achieve the goal:

In the build definition (that you specified in the build validation) add a PowerShell task that check the source branch of the pull request. When the source branch is not what you want the build will fail.

For example, in the following script, if the source branch is not test the build will fail, so add it in the master branch build validation:

$sourceBranch = "$(System.PullRequest.SourceBranch)"

if($sourceBranch -ne "test")
{
    exit 1
}
like image 95
Shayki Abramczyk Avatar answered Oct 24 '22 05:10

Shayki Abramczyk


I solved my problem via 3rd party web hook integration.

I developed nodejs expressapp and host it different server and i created integration rule which triggered when the pull request created. And i check source and target branches in my node app.

After that i added this integration to merge policies.

So cool , so easy. Thanks to everyone.

like image 1
Toygar Avatar answered Oct 24 '22 04:10

Toygar