Dexter
Dexter

Reputation: 1711

Git pre-merge hooks?

For my repository, I am using Git and Stash. On the Stash end, I have restricted (read only) access to master, so that any user can branch off the master branch for feature/ branches but cannot merge to master directly unless its done via a Pull request.

But as a user, I can accidentally merge my feature branch to the master branch and attempt to push the master branch.

The good thing is that, the push isn't allowed and is restricted by Stash, but I was wondering if there was a way I could restrict the user to merge any branches to the master locally with the help of some hooks.

I was trying out pre-commit hooks, and they are great, I was wondering if there was something similar to it, such as pre-merge hooks.

Upvotes: 7

Views: 2299

Answers (2)

Biswajit_86
Biswajit_86

Reputation: 3739

You can write a pre-commit or pre-push hook for this. I would prefer pre-push since you can use the commit to merge a feature branch into master in your local and test integration changes before pull request.

To create the pre-push hook, you can either ask developers to create a pre-push hook everytime they clone the repository, which is a very repetitive task. Instead, create a you can create a pre-push hook in the hooks directory of where your git binaries are installed and that would ensure that developers always get the hooks irrespective of how many times they clone or init a repository

Upvotes: -1

charleso
charleso

Reputation: 1836

I was wondering if there was a way I could restrict the user to merge any branches to the master locally with the help of some hooks.

Ex-Stash developer (not that it matters).

As @Zeeker mentioned you need to have each developer add hooks to their local repository. And in facts it's worse than that. Let's say you do the following:

git checkout -b this-is-not-master
...
git commit -m "This is not on master"

But then you do this:

git push origin this-is-not-master:master

At what point was the user "on" master? Basically you can't do it - local branches have nothing to do with remote branches, except that it's convenient for us to share the same name sometimes. If you need/want to restrict these things I would stick to adding hooks to Stash.

Upvotes: 3

Related Questions