Git repositories for multiple users

This is a simple guide to setting up Git repositories in which multiple users of a server can have access to. I've been running into many problems setting these up myself, so this is more of a reminder to myself and may be of some use to others.



Setting up a group of users

First, we need to prepare the users by grouping them into a project group so we can control access via this group. For our project, we'll create a group (here, "git") and add the users to the group.

(Note: The following actions happen on the server)

# create the git group
groupadd git
# add users to the group
groups user1 -aG git
groups user2 -aG git

Creating a bare git repository

We set up an empty project named gitproject. I'm setting up git projects in /var/git, but you may do this somewhere else, such that it is accessible to all users as well.

(The following actions happen on the server)

cd /var/git
mkdir gitproject.git
cd gitproject.git
git init --bare

We can now make sure that this directory is accessible to the group members

cd /var/git
chgrp -R git gitproject.git
chmod -R g+swX gitproject.git

Cloning the repository, making it the "origin" repository

Now we can come back to our local machine, create a local git repository, and start pushing and pulling code from/to the "origin" repository

First, we'll set up a local git repository.

(All of the following actions happen on your local machine)

mkdir gitproject
cd gitproject
git init

Now, we add our server's repository as the remote repository for our project:

git remote add origin user1@remoteserver:/var/git/gitproject.git

so we can pull the "master" branch at this "origin" repository:

git pull origin master

At this moment, your local development directory contains the same stuff as there is in the remote git repository, and you can start committing code to your local repository and pushing that into the remote repository (using git push origin master) and pulling updates from the remote repository into your local repository (using git pull origin master).

If you add the following three lines to your "./.git/config" file you no longer have to provide the "origin master" repo/branch details at every push or pull:

[branch "master"]

       remote = origin
       merge = refs/heads/master

Basic git commands

And now for some basic git commands:

Receiving changes

git pull # pull diffs from remote repo to local repo

Committing changes:

git commit -am "my comment" # commit to local repository
git push # push diffs from local to remote repo

Adding a new file

git add newfile
git commit -am "added newfile"
git push

comments powered by Disqus