This post describes how to setup your ubuntu server as a GIT server. Clients are able to clone repositories and push their changes back to the server with this setup. We make use of ssh in combination with the git-shell to provide extra security.
On your GIT server execute the commands below:
sudo apt-get install git sudo adduser git vi /etc/shells # add "/usr/bin/git-shell" vi /etc/passwd # change shell for user git to "/usr/bin/git-shell" touch /home/git/.hushlogin # prevent welcome message on ubuntu system mkdir /home/git/git-shell-commands vi /home/git/git-shell-commands/no-interactive-login # add text below #!/bin/sh printf "\nInteractive shell access not provided; go away!\n\n" exit 128 #end add text chown -R git:git /home/git/git-shell-commands chmod u+x no-interactive-login
If you use a Synology server add the following line to your /etc/shell file:
/var/packages/Git/target/bin/git-shell
Create a bare GIT repository on the server at /opt/git:
mkdir /opt/git mkdir /opt/git/the_project.git cd /opt/git/the_project.git git --bare init chown -R git:git /opt/git
Now on your local machine create a repository and push it to the server:
cd ~/ mkdir the_project git init git add . git commit -m "Initial commit" git remote add origin git@yourgitserver:/opt/git/the_project.git git push origin master
To clone the repository to your local development machine execute the following commands:
git clone git@yourgitserver:/opt/git/the_project.git # this will clone the repository into a directory "the_project". git status # check the status of the repository git log # check the log messages git tag # list the tags available git checkout v1.0.0 # check out the v1.0.0 tag # With the "git status" command you can see that the HEAD is detached: # HEAD detached at v1.0.0 git checkout master # back to HEAD version # No longer a detached head message when you execute "git status": # On branch master
When you have made changes to a file you want to undo (revert changes back to the last commit) execute the command below:
git checkout -- yourfile.txt
GIT and SSH public / private key
If you want to use git with a SSH key follow these steps. A SSH key is a convenient (and very secure!) way to login to your server. First create a public / private key pair with the command (-t is the type of encryption and -C is a comment):
ssh-keygen -t rsa -C "git @ repo machine"
Protect this key with a strong passphrase. After the key is generated you have to copy the public part to the server. Off course this should also be done in a secure way so use the ssh-copy-id tool for this. Execute the command:
ssh-copy-id -i gitkey git@yourmachinehis will copy the private key to the user home directory in a folder .ssh with the name authorized_keys. Now ssh to your machine with the command:
ssh git@yourmachine
The ssh_agent will ask for your passphrase (not the user password) and remember it. If you don’t have a ssh_agent enabled you could do without the passphrase but that is a bit less secure.