Git

Setting up a git server

First create a repo on any machine. If you already have one, use it. Otherwise use

    cd ~/myproject
    git init
and then maybe check in some files:
    echo 'hello' > foo
    git add foo
    git commit

Next create a bare repo clone of the existing repo:

    cd ~/myproject/..
    git clone --bare myproject myproject.git
this creates the directory myproject.git from the existing depot myproject/.git

Now copy myproject.git to the server. Choose a directory for it to live in (e.g. /src/mygit):

    ssh <user>@<server> mkdir -p /src/mygit
    scp myproject.git <user>@<server>:/src/mygit/myproject.git

That's it! Now to clone the repo go to a fresh dir on any machine use

    cd ~
    mv myproject myproject.old
    git clone <user>@<server>:/src/mygit/myproject.git
This will create a new ~/myproject directory fully populated. As a sanity check diff the new and old dirs:
    ddiff myproject myproject.old
If they are identical (or the old one does not contain anything useful that is nt in the new) then you can delete the old myproject.old directory.

To get the latest changes from the server:

    cd ~/myproject
    git pull
or
    git fetch
or
    git fetch origin

To push all changes from the local master branch back to the server's master branch:

    cd ~/myproject
    git push origin master

Adding a group for write access

If you will give some people read access and other people write access you should create a group for write access permission.

  1. Create a group to use for the repository. For example gitgroup:
        sudo groupadd gitgroup
    
  2. Add a hook script to preserve these permissions. On the server:
        cd /src/mygit
        echo "chgrp --preserve-root --quiet -R gitgroup /src/mygit" >> hooks/post-receive
        echo "chmod --preserve-root --quiet -R g+rw /src/mygit" >> hooks/post-receive
    
  3. Change the group and permission of all the dirs/files in the git repo on the server:
        cd /src/mygit
        chgrp -R gitgroup .
        chmod -R g+rw .
    

Granting access to a git server

To give someone access to git on a server:

  1. Create an account for them. For example using 'gituser' as username:
        
        sudo useradd -mN -s /usr/bin/git-shell gituser
    
  2. Add their public key to that account
        
        sudo su gituser -s /bin/bash
        cd
        mkdir -p .ssh
        cat rsa.pub >> .ssh/authorized_keys
        exit
    
  3. Create a git repo (ignore this to use an existing repo). For example using /src as location:
        sudo mkdir -p /src/mygit
        sudo chown <yourself>:<yourself> /src/mygit
        # now as user <yourself> do:
        cd /src/mygit
        git clone --bare /location/of/git/repo/to/copy  /src/mygit
    

This should give them read-only permission. (To grant read/write permission see below.)

Add as many keys to the ~gituser/.ssh/authorized_keys as you want - one (or more) for each person with access. They can now clone from

    git clone ssh://gituser@apooley-6/src/mygit

Granting write access to a git server

Create a group for write access (see above).

Add the user to the group:

    sudo usermod -a -G gitgroup gituser