Managing git repository with Unix-like permissions

April 2018 ยท 2 minute read

You want to handle git permissions with your unix filesystem. I’m not saying you should but if you want to here’s how to do it. For each git-repository you create a Unix group. Each user who needs commit access is part of this group.

First of create your repo with;

git init --bare repo-name
cd repo-name

You should normally use --bare when you are on the server/machine/whatever hosting the repository. The reason being that --bare instructs git to not use working directory structure. No one commits directly in this directory. It’s entirely optional.

After this you issue git config core.sharedRepository group. What this does is to instruct git to use the group permission whenever someone pushes a commit. They will be using the ssh protocol with their user identities. Hereafter group permissions need to be set with the group the repository is to be part of. We will use testgroup for demonstration purposes.

chgrp -R testgroup .
chgrp -R g+w .
chmod g-w objects/pack/*

Every file and folder is now part of the testgroup. Furthermore every file and folder is now writeable to the testgroup group.

find -type d -exec chmod g+s {} +

Finally we set the sticky-bit for group. What this does is to ensure that new files pushed get the testgroup group. Effectively making all users of the group able manipulate the pushed files.

This is how to setup permissions for a git repository on a Unix-like machine.

To recap the commands used in order;

git init --bare repo-name
cd repo-name
git config core.sharedRepository group
chgrp -R testgroup .
chgrp -R g+w .
chmod g-w objects/pack/*
find -type d -exec chmod g+s {} +