Start a collaborative project with git, in a remote repository

Sometimes I need to develop software with other people and the best way to do this is use Git (and this is not the only reason!).
Git is an awesome distribuited version control system made by Linux Torvalds and other cool guys :)
The goal of this howto is to setup a remote git repository to permit developers to work on a project (or more), everything under SSH (with public-key authentication).
It will work like this: in the remote server there will be one user, “git”, with it’s own home where to store repositories and it will have the git-shell, a restricted login shell for Git-only SSH access.
Access will be provided with ssh key authentication so each developer must have the private key, or you’ll have to create different keys for each developer.
The bad part is that i haven’t found a way to tell git which key i want to use, it want the standard key, ~/.ssh/id_rsa.
Let’s see what to do.

Install Git on your server, add an user/group called git or whatever you want and set the git-shell for it (not bash, ksh..).
Since the git user doesn’t have a shell you’ve to create the Git project from root:
cd /home/git
mkdir project
cd project
git --bare init .
chown -R git:git /home/git/*

Now that we have created the repository it’s time for the SSH keys: ssh public key authentication.
Create the .ssh dir in /home/git and set up the authorized_keys file:

mkdir /home/git/.ssh
mv /where/it/is/id_rsa.pub /home/git/.ssh/authorized_keys
chown -R git:git /home/git/.ssh
chmod 600 /home/git/.ssh/authorized_keys

Now you should have access in the project directory with SSH public-key auth.
In your local machine, go to an empty directory and try to clone the (empty) repository:

mkdir /tmp/test
cd /tmp/test
bash-4.1$ git clone ssh://git@ip_or_nameofserver.net/home/git/project .
Initialized empty Git repository in /tmp/test/.git/
warning: You appear to have cloned an empty repository.
bash-4.1$

Now you’re a developer, congrats!
Set your name/email:

git config --global user.name simplex
git config --global user.email simplex@twopenguins.it

Create a file and put it in the repository:

touch empty_file

Add it in the repo:

git add empty_file

Commit it, with a comment:

bash-4.1$ git commit -m "added empty_file for a quick test"
[master (root-commit) 20cfbcf] added empty_file for a quick test
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 empty_file
bash-4.1$

And push it to the remote repository, if it’s the first push, use git push origin master:

bash-4.1$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 229 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git@git.twopenguins.it/home/git/project
* [new branch] master -> master
bash-4.1$

Now another developer could clone the repository:

bash-4.1$ git clone ssh://git@ip_or_nameofserver.net/home/git/project .
Initialized empty Git repository in /tmp/coolproject/.git/
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
bash-4.1$ ls
empty_file
bash-4.1$

And then set his own name/email:

git config --global user.name supermario
git config --global user.email supermario@twopenguins.it

Create and/or edit files and commit:

touch empty_file2
touch empty_file3
echo "blabla" > empty_file

bash-4.1$ git add * && git commit -m “added and edited”
[master 5eab8e2] added and edited
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 empty_file2
create mode 100644 empty_file3

bash-4.1$ git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git@git.twopenguins.it/home/git/project
20cfbcf..5eab8e2 master -> master

Before editing something it’s a good thing to sync your local repository with the remote one with git pull.
Assuming you’re now the “first” developer:

bash-4.1$ cd /tmp/test/
bash-4.1$ ls
empty_file
bash-4.1$ git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ssh://git.twopenguins.it/home/git/project
20cfbcf..5eab8e2 master -> origin/master
Updating 20cfbcf..5eab8e2
Fast-forward
empty_file | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 empty_file2
create mode 100644 empty_file3
bash-4.1$ ls
empty_file empty_file2 empty_file3

If you do a git log you who did what and when:

commit 5eab8e200418fa3e75d33ad6c80c785f0cf1b601
Author: supermario
Date: Sat Mar 12 11:43:37 2011 +0100

added and edited

commit 20cfbcfbc17e113131a7b2f9fbca27fba758a038
Author: simplex
Date: Sat Mar 12 11:35:34 2011 +0100

added empty_file for a quick test

That’s all, for a complete guide to Git go to http://git-scm.com/documentation.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *