Kofi Black
Kofi Black

Reputation: 121

User Access Control in git

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?

Upvotes: 5

Views: 4284

Answers (2)

CodeWizard
CodeWizard

Reputation: 142612

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
}

Upvotes: 3

desoares
desoares

Reputation: 861

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).

Upvotes: 3

Related Questions