Create a shared Git repository

I was looking recently at options to set up a simple shared Git repository for one of the web projects I worked on.

My initial requirement was to make this work using local Ubuntu users and groups (I am aware of tools that can manage Git users and groups better but did not have time to investigate this further at that time - I use serevice provided by Bitbucket), and users would be able to push and pull over SSH (that was running on the machine already) between theirs local development repository and remote main repository.

Below I give steps for two scenarios that can be used to create Git shared repository for a project. I tested both and they worked for me well.

Create new empty shared Git repository

Steps cover:

  • set up of an empty bare Git repository,
  • clone of it from remote machine,
  • push and pull to remote repository from local development machine
  1. Set up new local group that would be allowed to push new changes and empty Git bare repo:
    addgroup <groupname>
    git init --bare --shared=group <nameofrepo>.git
  2. Set up ownership and permissions correctly
    chgrp -R <groupname> <nameofrepo>.git
  3. Set up user(s) and add it to <groupname> group:
    adduser <username>
    usermod -aG <groupname> <username>
  4. Clone shared repository to local machine
    git clone <username>@<server>:/path/to/git/repo[.git] <directory_name>
  5. Push and pull changes test
    pull origin
    push origin master

Create shared git repository for exiting project

Steps cover:

  • set up of bare repository for existing project on the same server
  1. turn existing project into Git repository
    cd /path/to/project
    git init
  2. clone this repository on the same server into bare Git repository that will be shared - main bare repository for this project
    git clone --bare --no-hardlinks /path/to/exiting/git/repo path/to/new/bare/git/<reponame>.git
  3. To make a private bare repo group shared: edit config file and add sharedRepository = group to the [core] section
  4. Fix permissions and ownership:
    chgrp -R <groupname> <reponame>.git
    cd <reponame>.git
    find . -type f | xargs chmod g+w
    find . -type d | xargs chmod g+ws

When creating shared Git repository make sure that:

  1. File permissions and ownership is set correctly on the shared repository
  2. In config file under [core] section we have line that says: "sharedRepository = group" or true
  3. Shared remote repository should be bare repository and should end with .git extension

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.