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
msysGit
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.
TortoiseGit
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.
Read here – https://github.com/multitheftauto/multitheftauto/wiki/how-to-use-tortoisegit – How to use TortoiseGit
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.
[codesyntax lang=”bash”]
sudo apt-get install git-core
[/codesyntax]
if you want gui you may also want to install git-gui
[codesyntax lang=”bash”]
sudo apt-get install git-gui
[/codesyntax]
[optionaly] Install git-doc
[codesyntax lang=”bash”]
sudo apt-get install git-doc
[/codesyntax]
To check version of Git installed run
[codesyntax lang=”bash”]
git –version
[/codesyntax]
Now you are ready to create repositories on Ubuntu.
Introduce yourself to Git
[codesyntax lang=”bash”]
git config –global user.name “Your name”
git config –global user.email “youremail@gmail.com”
[/codesyntax]
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:
[codesyntax lang=”bash”]
mkdir myproject
cd myproject
git init
[/codesyntax]
Add your files to the repository and commit changes:
[codesyntax lang=”bash”]
git add .
git commit -m “message”
[/codesyntax]
Now it is time to share your repositories between your local PC and Ubuntu server.
Share Git Repositories
You have many ways to share git repositories.
The most common way is using SSH.
Git over SSH
Setting up public/private key for GIT:
On server (Ubuntu)
1. Generate the keys
[codesyntax lang=”bash”]
cd ~/.ssh
ssh-keygen -t rsa
[/codesyntax]
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:
[codesyntax lang=”bash”]
cd ~/.ssh
cat id_rsa.pub >> authorized_keys
[/codesyntax]
4. Set permissions on your ~/.ssh folder:
[codesyntax lang=”bash”]
chmod 700 ~/.ssh
[/codesyntax]
5. Set permissions on your ~/.ssh/authorized_keys:
[codesyntax lang=”bash”]
chmod 600 ~/.ssh/authorized_keys
[/codesyntax]
6. Edit the file /etc/ssh/ssh_config and add the line
PubkeyAuthentication yes
7. Restart the ssh daemon
[codesyntax lang=”bash”]
sudo /etc/init.d/ssh restart
[/codesyntax]
Locally:
- Copy
id_dsa
andid_dsa.pub
from 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)
Another way.
You have another way to generate public/private keys – generate keys locally using puttygen.exe and copy public key to Ubuntu Linux server.
Locally:
- 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 —- “
On server:
- 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:
[codesyntax lang=”bash”]
chmod 700 ~/.ssh
cd ~/.ssh/
cat mykey.pub >> ~/.ssh/authorized_keys
chmod 600 authorized_keys
[/codesyntax]
Use your private key (mykey.ppk) located locally to connect to the server.
Read more how to setup and troubleshoot SSH authentication in my article.
Now you can work (push and pull) with your Git repositories located at Ubuntu server.
Locally in msysgit command line tool:
[codesyntax lang=”bash”]
git clone ssh://username@myserver.com/myprj.git
[/codesyntax]
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
Configure Apache
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)
[codesyntax lang=”bash”]
SetEnv GIT_PROJECT_ROOT /home/username/git SetEnv GIT_HTTP_EXPORT_ALL ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
[/codesyntax]
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
[codesyntax lang=”bash”]
<Location /git> AuthType Basic AuthName "Git" AuthUserFile /etc/apache2/passwd.git Require valid-user </Location>
[/codesyntax]
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.
[codesyntax lang=”bash”]
htpasswd -c /etc/apache2/passwd.git username
[/codesyntax]
If the file already exists then run httpasswd without -c option.
Restart Apache
[codesyntax lang=”bash”]
sudo service apache2 restart
[/codesyntax]
Craete bare repository
Create an empy bare git repository under the specified GIT_PROJECT_ROOT (/home/username/git in our example)
[codesyntax lang=”bash”]
cd /home/username/git
mkdir myproject
cd myproject
git –bare init
[/codesyntax]
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).
[codesyntax lang=”bash”]
sudo chown -R www-data:www-data myproject/
[/codesyntax]
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
[codesyntax lang=”bash”]
git clone http://username@yourserver.com/git/myproject
[/codesyntax]
Make some changes locally and commit changes to the server
[codesyntax lang=”bash”]
cd my project
# make some changes, add files, etc, touch readme
git add .
git commit -m “first commit”
git push origin master
[/codesyntax]
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
Restart apache2
$ sudo service apache2 restart
Try to clone the repository into some directory:
$ git clone http://username@git.yourdomain.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 DAV
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:
DAVLockDB /var/lock/apache2/DAVLock
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):
[codesyntax lang=”bash”]
<VirtualHost *:80>
ServerAdmin webmaster@mysite.com
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>
[/codesyntax]
Or you can do this without virtual hosts:
[codesyntax lang=”bash”]
<Location /my-repo.git>
DAV on
AuthType Basic
AuthName “Git”
AuthUserFile /etc/apache2/passwd.git
Require valid-user
</Location>
[/codesyntax]
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.
Add passwords
$ 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://username@git.mysite.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://www.kernel.org/pub/software/scm/git/docs/howto/setup-git-server-over-http.txt
* 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.
Install gitosis
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.
Read this tutorial showing how to host and manage Git repositories with access control, easily and safely.
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
tarabyte says:
This doesn’t specify what SSH client we should use for TortoiseGit.
‘TortoisePLink, coming from Putty, integrates with Windows better’ or ‘OpenSSH, Git default SSH Client’ which is NOT the default option. I’m going to choose OpenSSH for now…
tarabyte says:
under Git SSH:
On the Server:
I would make the paths a little more explicit by using this line instead:
$ cat ~/.ssh/id_dsa.pub > ~/.ssh/authorized_keys
admin says:
Makes sense. I’ve just changed it.
thank you.
Anonymous says:
Makes sense. I’ve just changed it. thank you.
tarabyte says:
WOW! thanks for the tutorial. I just got it working with Ubuntu Server, msysGit and TortoiseGit.
Andy Wright says:
Hi,
I believe this is a typo:
ssh-keygen -t ds
Should be:
ssh-keygen -t dsa
Hope this helps…
Anonymous says:
you’re right. thank you.
TaMereSuceDesOurs says:
there is no ssh directory
Anonymous says:
Then create the directory:
cd ~/
mkdir .ssh