GIT is a decentralized version control system. GIT differs from CVS, SVN, and various other VCS’s in that there is no need for a central repository though one can be used if desired.
I decided to use Git rather than SVN for source control for my new projects (LAMP and .NET).
Here are instructions I wrote to help to get started with Git on Linux (Ubuntu) and Windows.
- Install and configure msysGit on Windows
- Install Git on Ubuntu
- Share Git repositories
- Working with Git
- Install gitosis
Finally, we will have:
– Work repositories on Windows local machine
– Repositories on Ubuntu server (it can be Ubuntu on your virtual machine or Ubuntu on Amazon EC2 instance).
Install and configure msysGit on Windows
Install git by downloading the latest msysGit installer from http://code.google.com/p/msysgit
Run the installer and proceed through the wizard.
Selct the following options in wizard:
– The default setting is to “use Git Bash only” which means you will only be able to use git commands from within the Bash shell. Personally, I also like to run the git commands from inside a windows command prompt or a PowerShell window, so I choose the second option “Run git from within the Windows Command prompt.”
– you will be asked which SSH client you wish to use. Choose ‘OpenSSH’
– Choose ‘Use Windows style line endings’.
If you don’t need to share your projects with other developers you can stop here and use Git repositories locally.
If you want to work with Git repositories on Windows – you may want to install TortoiseGit.
TortoiseGit (like TortoiseSVN and CVS) is accessed through the explorer context menu, which can be opened by right clicking inside any folder in Windows Explorer.
Install Git on Ubuntu
Next step is to setup Git on Ubuntu Linux servers. Process of installing Git is the same for Ubuntu on virtual machine and Amazon EC2 instance.
sudo apt-get install git-core
if you want gui you may also want to install git-gui
sudo apt-get install git-gui
[optionaly] Install git-doc
sudo apt-get install git-doc
To check version of Git installed run
Now you are ready to create repositories on Ubuntu.
Introduce yourself to Git
git config --global user.name "Your name" git config --global user.email "firstname.lastname@example.org"
You only need to do this once.
Create new repository
Start your project just by making a new directory.
Go to your project directory and init your git repository:
mkdir myproject cd myproject git init
Add your files to the repository and commit changes:
git add . git commit -m "message"
Share Git Repositories
You have many ways to share git repositories.
Git over SSH
Setting up public/private key for GIT:
On server (Ubuntu)
1. Generate the keys
cd ~/.ssh ssh-keygen -t rsa
2. Accept the file names it wants to use. Don’t enter a passphrase (just enter). You can change a passphrase later.
3. Add the pub key to the authorized_keys file:
cd ~/.ssh cat id_rsa.pub >> authorized_keys
4. Set permissions on your ~/.ssh folder:
chmod 700 ~/.ssh
5. Set permissions on your ~/.ssh/authorized_keys:
chmod 600 ~/.ssh/authorized_keys
6. Edit the file /etc/ssh/ssh_config and add the line
7. Restart the ssh daemon
sudo /etc/init.d/ssh restart
id_dsa.pubfrom the server to your local hard drive (use winscp or sftp or some such tool) c:\users\userName\.ssh directory (this is for Windows 7)
- !!! IMPORTANT: you may need to convert the keys to the format that putty understands. To do this run puttygen.exe and load your key. Then save your private key (id_rsa.ppk) to your local hard drive (c:\users\username\.ssh\id_rsa.ppk).
- Set tortoise git to point to C:\Program Files\Git\bin\ssh.exe (not putty)
You have another way to generate public/private keys – generate keys locally using puttygen.exe and copy public key to Ubuntu Linux server.
- Run puttygen.exe (D:\Program Files\TortoiseGit\bin\puttygen.exe)
- Generate a key pair
- Save private and public keys to your local hard drive (usually in folder c:\Users\USERNAME\.ssh\): mykey.ppk, mykey.pub
- !!! IMPORTANT: check that your public key does not have comments in text and it has one line of text starting with “ssh-rsa”. You may need to remove these lines at the beginning:”—- BEGIN SSH2 PUBLIC KEY —- Comment:”and remove at the end: “—- END SSH2 PUBLIC KEY —- “
- Public key should be kept in SSH server. Copy your public key file (mykey.pub) to the server to ~/.ssh/mykey.pub.
- Run these commands on the server:
chmod 700 ~/.ssh cd ~/.ssh/ cat mykey.pub >> ~/.ssh/authorized_keys chmod 600 authorized_keys
Use your private key (mykey.ppk) located locally to connect to the server.
Now you can work (push and pull) with your Git repositories located at Ubuntu server.
Locally in msysgit command line tool:
git clone ssh://email@example.com/myprj.git
To know more about SSH keys – read this Ubuntu documentation.
Git over HTTP
Sometimes git needs to be able to work over http. SSH is not a fast protocol for making frequent changes to a remote repository, so you may want to get access to your Git repositories through http.
Traditionally git used to work only over ssh or git protocols while there was only a dumb version of git over http which was slow and inefficient. Sometimes git needs to be able to work over http. Now starting from git 1.7 both git servers and clients have support for smart http which works over http(s) and is supposed to be as efficient as the ssh version.
The old ‘dumb’ transport method (using only DAV), git can not request specific objects from a server, it can only find out what packfile contains the object it needs and download the entire packfile. Also this protocol doesn’t call hooks. The older ‘dumb’ transport was also never really intended for pushing changes to the server.
I use http protocol for git repositories which require frequent updates while testing and debugging. For example, I have several projects which I cannot test locally (for example, facebook applications) and I need to upload changes to the remote server.
Git over HTTP: Smart HTTP
Make sure mod_cgi, mod_alias, and mod_env are enabled.
Open the Apache config file and add the following lines (/etc/apache2/apache2.conf or /etc/apache2/httpd.conf by default)
SetEnv GIT_PROJECT_ROOT /home/username/git SetEnv GIT_HTTP_EXPORT_ALL ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
The GIT_PROJECT_ROOT should point to the root folder where git repositories would be hosted. Set this away from the document root of the web server.
What the above do is direct any requests with /git/ to the git-http-backend and tell the script that the root of git repositories is GIT_PROJECT_ROOT.
Append the following to theApache config file to enable read/write access to the folder
<Location /git> AuthType Basic AuthName "Git" AuthUserFile /etc/apache2/passwd.git Require valid-user </Location>
What the above do is make requests to /git only accessible to valid users and tell valid users are listed on the file /etc/apache2/passwd.git.
Make sure the file ‘/etc/apache2/passwd.git’ is accessible by Apache (user ‘www-data’).
Use the following command to create the user list at /etc/apache2/passwd.git and add the user ‘username’ with a password to it.
htpasswd -c /etc/apache2/passwd.git username
If the file already exists then run httpasswd without -c option.
sudo service apache2 restart
Craete bare repository
Create an empy bare git repository under the specified GIT_PROJECT_ROOT (/home/username/git in our example)
cd /home/username/git mkdir myproject cd myproject git --bare init
Set permissions on git repository
Make the folder ‘myproject’ with it’s subfolders owned by the user which the web server is run from (user ‘www-data’ in Ubuntu).
sudo chown -R www-data:www-data myproject/
Now the bare git repository should be pull-able and pushable by authorized users.
User the repository
Clone the git repository over http from another location
git clone http://firstname.lastname@example.org/git/myproject
Make some changes locally and commit changes to the server
cd my project # make some changes, add files, etc, touch readme git add . git commit -m “first commit” git push origin master
The manual for the git-http-backend can be found here.
Git over HTTP: DAV
I followed instructions written in this post with a few modifications.
setup a bare GIT repository
Create the directory under the DocumentRoot of the directories served by Apache. As an example we take /var/www.
$ cd /var/www
$ mkdir git
$ cd git
$ mkdir my-repo.git
Initialize a bare repository
$ cd my-repo.git
$ git –bare init
Change the ownership to your web-server’s credentials.
$ cd /var/www
$ chown -R www-data:www-data git
If you do not know which user Apache runs as, you can alternatively do a “chmod -R a+w .”, inspect the files which are created later on, and set the permissions appropriately.
Enable the post-update hook:
$ cp /var/www/git/my-repo.git/hooks/post-update.sample /var/www/git/my-repo.git/hooks/post-update
$ chmod +x /var/www/git/my-repo.git/hooks/post-update
$ sudo service apache2 restart
Try to clone the repository into some directory:
$ git clone http://email@example.com/my-repo.git myrepo
If you get this error:
fatal: http://git.yourdomain.com/my-repo.git/info/refs not found: did you run git update-server-info on the server?
then you need to init your repository references:
$ cd /var/www/git/my-repo.git/
$ sudo -u www-data git update-server-info
Enable the dav and dav_fs modules of apache:
$ a2enmod dav_fs
$ a2enmod dav
The DAV lock is located in /etc/apache2/mods-available/dav_fs.conf:
This directory to be writable by the user Apache runs as.
Setup GIT directories to enabled DAV
I am using a domain to access all my GIT repositories located in /var/www/git.
Add these lines to httpd.conf (/etc/apache2/httpd.conf):
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot "/var/www/git" ServerName git.mysite.com <Directory "/var/www/git"> DAV On AuthType Basic AuthName "git" AuthUserFile "/etc/apache2/passwd.git" Require valid-user </Directory> <Directory "/var/www/git/my-repo.git"> DAV on AuthType Basic AuthName "Git" AuthUserFile "/etc/apache2/passwd.git" Require valid-user </Directory> </VirtualHost>
Or you can do this without virtual hosts:
<Location /my-repo.git> DAV on AuthType Basic AuthName "Git" AuthUserFile /etc/apache2/passwd.git Require valid-user </Location>
Do not forget to init a bare repository in /var/www/git/my-repo.git as described before.
The password file can be somewhere else, but it has to be readable by Apache and preferably not readable by the world.
$ htpasswd -c /etc/apache2/passwd.git <user>
You will be asked a password, and the file is created. Subsequent calls to htpasswd should omit the ‘-c’ option, since you want to append to the existing file.
You need to restart Apache.
$ sudo service apache2 restart
Check in browser
Go to http://<username>@<servername>/my-repo.git in your browser to check whether it asks for a password and accepts your password.
Setup the client on Windows
You can access the repository from Windows as follows:
Run msysgit command line tool or Windows native command line tool (if you setup msysgit appropriately).
$ cd /path/to/project
$ git clone http://git.mysite.com prjname
You may see a warning “You appear to have cloned an empty repository”. You can just ignore this warning.
Add some changes to your project in /path/to/project/prjname.
Commit your changes to a local repository:
$ git add .
$ git commit -m “some message”
Now you can push your changes back to the server:
$ git remote add web http://email@example.com/my-repo.git
$ git push web master
Here you will be promted to enter a password. You may want to save your password:
$ git remote add web http://username:PASSWORD@git.mysite.com/my-repo.git.
Of course it is not recommended to save the password this way.
Read more about sharing GIT repositories:
* 8 ways to share your git repository – provides a summary of different ways to share a git repository.
* http://progit.org/book/ch4-1.html – the basic strengths and weaknesses of each protocol
Working with Git
Read this article to know basic commands with Git repositories.
gitosis is a tool for hosting GIT repositories. It manages multiple repositories under one user account, using SSH keys to identify users. However, users do *not* need shell accounts on the server, instead they will talk to one shared account that does not allow arbitrary commands. Git itself is used to setup gitosis and manage the Git repos.
Git Work Flow
I am using remote Git repositories to deploy my projects to the remote host (Ubuntu server installed on Amazon EC2 instance).
Read the details in my post “GIT Work Flow and Deployment of sites using Git”
Read more about Git:
* http://www.kernel.org/pub/software/scm/git/docs/user-manual.html – Git User’s Manual