Git is a wonderful distributed version control system and we can have a personal repository quickly set up without any technical difficulties. However at times we may need to work on shared development model where multiple people need commit access to the code. In such cases we would need a centralized repository store.
By following the given steps we will be able to set up a shared git repository accessible over SSH.
- First a user needs to be created to hold the git repository (multiple repositories can also be set up under the same git user). Next a password need to be set up for the same user. Since this user will never log on using shell, a password which is secure, random, very long string is a good option.
$ sudo /usr/sbin/useradd -c "Git Version Control" -d /home/git -m -s /bin/bash git $ sudo passwd git
- Next login as git user and create the project directory to hold the repository and then initialize that directory as a barebones (blank) git repository
$ mkdir -p /home/git/projects/myproject1.git $ cd /home/projects/myprojects.git $ /usr/bin/git --bare init
- Set up the .ssh directory for the git user to hold the authorized_keys file with the public ssh keys for the user allowed to log in a git user and access the repository
$ cd /home/git $ mkdir .ssh $ chmod -R 700 .ssh $ touch .ssh/authorized_keys $ chmod 644 .ssh/authorized_keys
- Finally change the shell of the git user to git-shell so that interactive logins for the user are not possible and only selected remote git commands are allowed.
$ /usr/bin/chsh -s /usr/bin/git-shell
Add the public key of the user(s) to the authorized key file. These users will be able to access the repository. Now all that needs to be done is to clone the repository and start working. Following command can be used to clone the repository on a *nix box. The 7652 is the port number where ssh daemon is listening. If your port is the default, 22, you can skip this port number.
$ git clone ssh://git@myhost.mydomain.com:7652/home/git/projects/myproject1.git/