How to Create a Git Bare Shared Repository
- Scenario
- Create directory that will contain the git repositories
- Create group
git
- Add users to group
git
- Change group ownership of git repo directory to group
git
- Create git repo bare with shared option
- Verify the new
helloworld.git
repo permissions - Check out the repo with
git clone
- Make changes and push to repo
- Conclusion
Scenario
You create a git repository using git init --bare
on the repo server. You are able to checkout the code from your local development computer. You can make changes, run git add, git commit, git push successfully. So far, everything is good.
You are the only one maintaining this repo.
Sometime later, you get to have or or more teammates join you in maintaining the repo code.
Unfortunately, they are unable to checkout the code and / or commit any changes to the repo.
That happened because your git repo was not created as a shared repository.
This blog post shows you how to create a brand new shared git repository from scratch and assign read or read-write permissions to other potential group members from the start.
Create directory that will contain the git repositories
Login to the remote server. I usually host my git repos either at /srv/gitrepos
or /var/gitrepos
.
Let us assume that the git repo will be hosted at /srv/gitrepos
and we do not have that directory yet. We will create it first.
sudo mkdir -p /srv/gitrepos
Create group git
On Debian based OSes (Debian, Ubuntu, Linux Mint), this command will create a group called git
.
sudo addgroup git
On all OSes, including Red Hat based OSes (RHEL, Fedora, CentOS), this command will create a group called git
.
sudo groupadd git
Add users to group git
We will add users harry
, ron
and hermoine
to the group git
.
sudo adduser harry git
sudo adduser ron git
sudo adduser hermoine git
Now, all three users belong to group git
.
Change group ownership of git repo directory to group git
We will change the group of /srv/gitrepos
to group git
.
sudo chgrp -R git /srv/gitrepos
Now, the directory /srv/gitrepos
will belong to group git
.
Create git repo bare with shared option
At this point, all permissions and ownerships are established. We will create a git bare repository helloworld.git
under /srv/gitrepos
with the shared option. The shared option will vary depending on the permissions.
/srv/gitrepos/helloworld
will have git
as its default group, inherited from the parent directory /srv/gitrepos
There are different read-write options based on the user, group and others. The assumption in this tutorial is that the user will have read-write, and groups and others will have varying levels of permissions when the git repo is created.
user | group | others | permission | |
---|---|---|---|---|
1 | read-write | read-write | read-write | 666 |
2 | read-write | read-write | read | 664 |
3 | read-write | read | read-write | 664 |
4 | read-write | read | read | 644 |
5 | read-write | read | NO | 640 |
6 | read-write | NO | read | 604 |
1) Allow user, group and others to have read-write access
The permission for this repository will be 666.
git init --bare --shared=0666 /srv/gitrepos/helloworld.git
2) Allow user and group to have read-write access, and others to have read-only access
The permission for this repository will be 664.
git init --bare --shared=0664 /srv/gitrepos/helloworld.git
3) Allow user and others to have read-write access, and group to have read-only access
The permission for this repository will be 646.
git init --bare --shared=0646 /srv/gitrepos/helloworld.git
4) Allow user to have read-write access, group and others to have read-only access
The permission for this repository will be 644.
git init --bare --shared=0644 /srv/gitrepos/helloworld.git
5) Allow user to have read-write access, group to have read-only access, and others to have NO access
The permission for this repository will be 640.
git init --bare --shared=0640 /srv/gitrepos/helloworld.git
6) Allow user to have read-write access, group to have NO access, and others to have read-only access
The permission for this repository will be 604.
git init --bare --shared=0604 /srv/gitrepos/helloworld.git
Verify the new helloworld.git
repo permissions
Verify that permissions of the new helloworld.git
repo.
ls -al /srv/gitrepos/helloworld.git
If the group is not owned by git, you can change the permission.
chgrp -R git /srv/gitrepos/helloworld.git
Check out the repo with git clone
Now, from your local computer, checkout the newly created git repository. Replace USER@SERVER
with the username and server hostname or IP address or your git repository server.
git clone SERVER:/srv/gitrepos/helloworld.git
You will see a directory helloworld
created in your current directory.
Make changes and push to repo
Create a README.txt file, add content to it, and check it in.
echo 'This README.txt file has nothing really.' > README.txt
Now, git add, git commit and git push the updates to the repo.
git add .
git commit -am 'Added a one line README.txt file'
git push
It should push successfully if all the permissions to read-write are set correctly.
Now, ask someone in your group or others to try it out. Depending on your read-write permission level, the appropriate action will be possible.
Conclusion
These sequence of steps worked for me serveral times while creating git bare repositories with shared option. Your setup may be different or vary slightly. Feel free to contact me if you are still unable to read or write to a shared git repo. Thanks for reading.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.