Setting up your own private GIT server

git user and a .ssh directory for the user. Follow the commands given below.

$ sudo adduser git
$ su git
$ cd
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
  Let’s add some SSH keys to the authorized_keys file for the git user. They should be like
$ cat /tmp/id_rsa.john.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4L
ojG6rs6hPB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4k
Yjh6541NYsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9Ez
Sdfd8AcCIicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myiv
O7TCUSBdLQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPq
dAv8JggJICUvax2T9va5 gsg-keypair
  We now append them to the git user’s authorized_keysfile in its .ssh directory.
$ cat /tmp/id_rsa.john.pub >> ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.josie.pub >> ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.jessica.pub >> ~/.ssh/authorized_keys
  We can now set up an empty repository by running git init with the –bare option, which initializes a repository without a working directory.
$ cd /srv/git
$ mkdir project.git
$ cd project.git
$ git init --bare
Initialized empty Git repository in /srv/git/project.git/
  Now one can push their first version of their project into that repository by adding it as a remote and pushing up a branch. We can now use a hostname of the server in which we have set up git user and repository.
$ cd myproject
$ git init
$ git add .
$ git commit -m 'initial commit'
$ git remote add origin [email protected]:/srv/git/project.git
$ git push origin master
  Now, others can clone the directory and push back the changes.
$ git clone [email protected]:/srv/git/project.git
$ cd project
$ vim README
$ git commit -am 'fix for the README file'
$ git push origin master
  We can restrict a user to have only git related activitis using a shell tool called git-shell. If we set the shell to a user’s login shell, he cannot have normal usage. To use, specify the git-shell instead of bash or csh for your user’s login shell.First add git-shell to /etc/shells if it does not exist.
$ cat /etc/shells   # see if `git-shell` is already in there.  If not...
$ which git-shell   # make sure git-shell is installed on your system.
$ sudo vim /etc/shells  # and add the path to git-shell from last command
 
Now you can edit the shell for a user using chsh <username> -s <shell>:
$ sudo chsh git -s $(which git-shell)
Now, the git user can only use the SSH connection to push and pull Git repositories and can’t shell onto the machine. If you try, you’ll see a login rejection like this.
$ ssh [email protected]
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to gitserver closed.
  If you are in need of higher control over your git repositories, setup your own private git server.]]>