Source control is essential to any software developer. SVN has always been the standard after CVS, but as soon as Linus Travolds introduced Git to the world, back in 2005, everything changed.
Git is a distributed source control software, and although it wasn't the first distributed system, it certainly proved itself; if it works for the largest open source software project (Linux Kernel) certainly it is that good.
But if you are comfortable with SVN and its centralized nature, git is flexible enough to support a work-flow similar to SVN.
In this article we will setup a git server on a Linux machine accessible through a network and ssh. This server can have many repositories for multiple users. The setup is excellent for small team that needs private git repositories without all the bells and whistles of large git cloud repository services like Github.
Following is our agenda:
Git User Setup
Single Repo Setup
Allow Team Access
Git User Setup
We will run the following commands inside the terminal of the git server. You will need a root privilege to execute these commands, so you can use sudo before each command that you can't run or print the following command first:
$ sudo bash
But be careful, using the previous command can damage your system if you don't know what the commands do.
$ mkdir /srv/git.repo
$ adduser --home /srv/git.repo --shell /bin/bash --no-create-home git
Adding user 'git' ...
Adding new group 'git' (1000) ...
Adding new user 'git' (1000) with group 'git' ...
Not creating home directory '/srv/git.repo'.
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
Full Name :
Room Number :
Work Phone :
Home Phone :
Is the information correct? [Y/n]
You will be asked some user details, you can go ahead and ignore them hitting Enter till you finish all of them.
Now you have created a new user on the server with username, password and home directory.
We will use this home directory to store each project repository as a different folder.
Single Repo Setup
So far so good. Now we need to add a repository.
The work flow is simple, you initialize a local git repository on any machine accessible to the git repo server.
$ git init /path/to/project/folder
$ cd /path/to/project/folder
$ git add .
$ git commit -m "Initial project commit."
Initializing and working with a local git repository is outside the scope of this article, so you can find more information on that on the official git documentation.
Next you need to make a special clone of this repository on the git repo server from our previous setp.
$ su git
$ git clone --bare /path/to/project/folder projectname.git
This will create a special git repository with the name projectname.git. In this repository you wouldn't see all the files and folders that you have commited in your own local project, instead these are the files needed by git to represent the project and the changes done to its files and folders.
Next step is to make sure that you can access the repository using ssh protocol. Because the setup of ssh vary from system to system, you will need to search the web to find how to activate the ssh daemon on the git repo server. You can test if the daemon is enabled or not by trying the following command.
$ ssh git@localhost
If the command asked for "git" password, then the ssh daemon works and you can proceed to the following step.
Allow Team Access
The following steps need to be done for each team member who needs access the repository.
Please note that this will allow each team member to access all repositories on the git repo server. If you need a separate account to different teams you will have to repeat the first step with a user for each different team.
Each team member will generate an ssh key, and send their public ssh key to the git repo server in order to authenticate their and allow to "push" their changes to the repo.
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Created directory '/home/user/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
As you can see, you don't need to provide any input, just hit Enter where promoted for input until you generate the key.
$ ssh-copy-id git@SERVER_IP
This command will send your public ssh key to the git repo server, so you need to replace the SERVER_IP with the actual IP of the server or the server domain name if it's accessible through DNS.
This command will ask you for "git" user password on the server, that's why each team member must know the "git" password, but doesn't need to remember it.
A workaround would be to manually add each team member public ssh key to the ~/.ssh/authorized_keys file on the git home folder.
To add the new repository as a remote to your current working repo use the following command.
$ git remote add origin ssh://git@SERVER_IP:/srv/git.repo/projectname.git
If you find yourself lost at any point or need help, you will find plenty of online resources to get help from. I personally recommend the official git documentation. as a good place to get started.
In a small to medium size team, working on the same remote repository in an SVN like workflow can work, but needs continuous integration and high level of communication in order to ensure that all conflicts are discovered and resolved as soon as possible.