4

I have a simple question. We are using Bitbucket as the git provider. Suppose I am the manager of a development team.

I want myself to be the only person who can merge code to master branch.
Other team members can checkout master branch and create new branches, but they cannot merge code to master branch. How can I do this in Git?

Kofi Black
  • 111
  • 1
  • 7
  • 2
    Various git providers, like GitHub, Bitbucket, Gitlab, would have the function to fulfill the requirement you described. Which provider are you currently using? – junkangli Apr 27 '18 at 16:05
  • The simplest way is to make your repo read-only to any user but yourself. That way, you can pull from your devs when they have something in their clone, but they can never push changes to your repo. – Mad Physicist Apr 27 '18 at 16:17
  • 2
    Possible duplicate of [How to restrict access to master branch on git](https://stackoverflow.com/questions/38864405/how-to-restrict-access-to-master-branch-on-git) – 1615903 Apr 27 '18 at 17:10
  • Hi junkangli, we are using Bitbucket. – Kofi Black Apr 29 '18 at 14:49

2 Answers2

3

In my experience the best way to do that is to allow the team only to fork the repository, than when a feature is read they submit a pull request(Github) or an merge request(Bitbucket).

desoares
  • 861
  • 7
  • 15
2

What you are asking is very simple to achieve but it depends on your way you work.

If you are using git server you can "protect" the desired branch from being merged.


Protect branches under github

enter image description here


Protect branches sunder bitbucket

Here you will have to choose prevent all changes and yourself as allowed user

enter image description here enter image description here


Git hooks

You can achieve it will a simple pre-receive hook again depends on your git server

For example:

#!/bin/sh

# Extract the desired information from the log message
# You can also use the information passed out by the central repo if its available

# %ae = Extract the user email from the last commit (author email)
USER_EMAIL=$(git log -1 --format=format:%ae HEAD)

# %an = Extract the username from the last commit (author name)
USER_NAME=$(git log -1 --format=format:%an HEAD)

# or use those values if you have them:
# $USER, $GIT_AUTHOR_NAME, $GIT_AUTHOR_EMAIL

if [ "$1" != refs/heads/master ] && [ CHECK_FOR_USER_NAME_OR_EMAIL ] {
    echo "ERROR:  you are not allowed to update master" >&2
    exit 1
}
Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    Seeing that you've covered hooks in your list of options, I'll back down a bit from my initial comment; but not entirely. Most people don't seem to find hooks "simple", and there is no single solution that is generic to "git servers". GitHub is not git; bitbucket is not git; gitlab is not git; and there's enough confusion around this point that I think it should be more clear than you've presented. – Mark Adelsberger Apr 27 '18 at 18:14