Tech Blog

Xiao's development notes

Windows GIT Tutorial: Cygwin, GIT, SSH and others

We want to install git and share code with our group, and we need to use our Windows machine. Sometimes, it looks easy, but in reality, it could be daunting.

First, we tried the GitStack. It has a pretty nice user interface, and the tutorial is pretty straightforward. After installation, I can connect to the server (which is also my machine), but it seems like others can’t push. Also we realized it charges a subscription fee for more than 2 users. OK, we have more than 2 people in our group. And if we want to pay for it, we need to wait for a long time just to request the purchase. GUI, goodbye.

We then started to try to use gitosis, which is an open source git software running on Windows. You’ll need to install “git”, “openssh”, “python” to do install the software.

Setting up SSH

Now that Cygwin is installed, we can use it to set up SSH as a service. This will be what we use to pull from and push to our git repositories later. Before setting the SSH service up, we must make a tweak to some privileges. Apparently this is a problem with newer Cygwin releases (from here). Run the following commands:


chmod +r /etc/passwd

chmod u+w /etc/passwd
<code>chmod +r /etc/group
chmod 755 /var

In order to get the SSH service set up, type ssh-host-config, and answer yes to the question "Should privilege separation be used?" In addition, answer yes when asked whether a new local account called 'sshd' should be created, and to whether sshd should be installed as a service. When prompted for the value of the 'CYGWIN' environment variable, accept the default by hitting enter, and answer no to whether you wish to use a different username to the default for the account for running the service, and then answer yes to whether the user account 'cyg_server' should be created. You will be asked for a password for the new user - enter your choice of password here. This is just to protect the user account from unauthorised access, since it will have local administrator privileges. If all goes well, your user account will be created and the service will be set up.
Then we started to use Cygwin and git with it. It is nice that my cygwin came with git. And installing ssh is easy too. Good, this way seems promising. Wait...... It will only work quickly if you are lucky. For us? After three days of hard work, it finally working now. To save you sometime, I wanted to write a tutorial. Maybe it is not exactly your situation, (because it is Windows!!!), at least there is hope that this will help.

Installing Python Setuptools

Before installing gitosis, we will need to install python setuptools. This is what gitosis uses to install itself. The latest version of setuptools can be found at the Python Package Index - scroll down near the bottom of the page for the files. Since we are installing in Cygwin, we want the .egg version matching the python version installed by Cygwin. For me, it was python 2.5 and the .egg file I needed was called setuptools-0.6c9-py2.5.egg. To check your python version, run python -V in the bash shell. To download the file, right click and save the target since many browsers try to interpret .egg files as text. I placed the file in root of the C drive for ease of access from the Cygwin Bash shell.

Once the download is finished, we can run the .egg file as if it were a shell script or executable file. Execute the following command (changing the path if you saved the file elsewhere or have a different version):

/cygdrive/c/setuptools-0.6c9-py2.5.egg

This will whizz through quickly and install setuptools for you. Now we're ready for installing gitosis.

Installing Gitosis

Gitosis is installed by cloning its git repository. In your Cygwin Bash window, create a convenient directory and change into it. You can then clone the gitosis repository. Since gitosis is small and lightweight, this will only take moments.

mkdir sources && cd sources
git clone git://eagain.net/gitosis.git

Once the clone is complete, install gitosis with the following commands:

cd gitosis
python setup.py install

A bunch of output will fly past and if all went well, gitosis is now installed.

After that, you still need to set up access for Gitosis. But most of the work on server side has been done. For more tutorial: http://markembling.info/2009/08/git-server-gitosis-and-cygwin-on-windows

Now, Let's get back to the client side.

What do you need to install on Cygwin:

1. OpenSSL

2. OpenSSH

3.git

<strong>(Make sure about your version of git!!! Reinstall some software if necessary.)</strong>

Now your Cygwin is ready. Next step is to make sure you can ssh to server. It doesn't matter if that's password-less login or not.

1. Generate your public key

username@hostname:~$ ssh-keygen
 Generating public/private rsa key pair.
 Enter file in which to save the key (/home/username/.ssh/id_rsa):
 Created directory '/home/username/.ssh'.
 Enter passphrase (empty for no passphrase):
 Enter same passphrase again:
 Your identification has been saved in /home/username/.ssh/id_rsa.
 Your public key has been saved in /home/username/.ssh/id_rsa.pub.
username@hostname:~$ cat ~/.ssh/id_rsa.pub

2. Let server know you

</pre>
cat .ssh/id_rsa.pub | ssh username@hostname 'cat >> .ssh/authorized_keys'
<pre>

After this, you should be able to use ssh username@hostname
Sometimes the connection will be terminated immediately, but it doesn't matter. There are a lot to get into with regard to SSH, let's not get there now.

Next, all the preparation work is done, we finally are going to work on "git". Go to the git folder you want to use as repository.

username@hostname:~/work$ git init
Initialized empty Git repository in /home/username/work/.git/
username@hostname:~/work$ git remote add project git@hostname:project.git
username@hostname:~/work$ git pull project master
 remote: Counting objects: 126, done.
 remote: Compressing objects: 100% (102/102), done.
 remote: Total 126 (delta 17), reused 111 (delta 15)
 Receiving objects: 100% (126/126), 184.87 KiB | 78 KiB/s, done.
 Resolving deltas: 100% (17/17), done.
 From git-CompanyName@freeN.projectlocker.com:project
 * branch master -> FETCH_HEAD

Now, you are synchronized with the master version on server. The problem I had was the error "git@hostname:project.git is not a git repository". Use the absolute path, don't forget colon!!!

OK. Now it gets really exciting. The next step is to push your code to central repository.

username@hostname:~/project$ vim test.txt
username@hostname:~/project$ git add test.txt
username@hostname:~/project$ git remote project
username@hostname:~/project$ git commit -m "adding test file"
username@hostname:~/project$ git push project master
 Counting objects: 4, done.
 Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 282 bytes, done.
Total 3 (delta 1), reused 0 (delta 0) To git-CompanyName@freeN.projectlocker.com:project.git a6a6bf9..4811b38
 master -> master

Yay!!! Congratulations, your code is on central server now. Something you might want to know too: Pull: git pull Checkout: git checkout HEAD /testDirectory.

</pre>
 username@hostname:~/project$ git pull origin master
username@hostname:~/project$ git checkout HEAD test.txt
<pre>

I'm not an expert of git. So this post is more for people who are just getting started with git on Windows. Yes, it is hard. But worth trying too. Now you can share code with friends, coworkers within a local network (company network).

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: