Archive for June 2009

 
 

Pimp my git

Now that I’m starting to use git more regularly I’ve started looking for ways to make git even better. It turns out that git’s really easy to customise.

Aliases

Having recently heard that “git stage” is going to be added as an alias for “git add” another git fan mentioned that you can add your own git aliases to the .git/config file. I particularly like this idea as some of the commands can be a little bit esoteric. To get you started try adding this entry to your .git/config file.

[alias]
  stage = add
  unstage = reset HEAD

Now you can add content to the staging area using

  git stage new_file

and then remove it again using

  git unstage new_file

Pretty log

This one’s an alias, just add it in the same way as stage/unstage

plog = log --pretty=tformat:'%h %Cblue%cr%Creset %cn %Cgreen%s%Creset'

I find this format much easier to read, colour coded information and you can fit lot’s more commits on screen. This makes the graph version of log awesome, take a look

git plog --graph

Win!

Colour coded status

Add the following to .git/config and staged files will be shown in green, unstaged in red.

[color]
  ui = auto

Command line prompt

The git-prompt project will let you customise your prompt to include all sorts of git information, the defaults are a bit full on but you can tame it to your taste easily enough

I’m sure I’ll keep adding to this list, let me know any great git tricks you’ve got.

Git rebase from another repository

git pull --rebase repository branch

Native ruby gems on ubuntu

If you’re having problems installing gems with native extensions on ubuntu you’ve probably seen something like this:

/usr/bin/ruby1.8 extconf.rb
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
	from extconf.rb:1

the fix…

sudo apt-get install ruby1.8-dev

(ruby1.9-dev is also available)

The native extensions should now build correctly. Apparently the extra dev resources aren’t installed by default with ruby 1.x so that the packages comply with debian standards…

Instant remote git repository using ssh

These instructions assume that you can do an automatic login from a local to a remote machine using ssh keys for user $username.

Remote

cd folder/to/become/repository.git
git init --bare

Local

cd folder/to/become/local/repository
git init
git remote add remote_name ssh://$username@remote.hostname/path/to/repository.git
git pull remote_name master

…and you’re done, you’ve just gotta love git!

Getting started with Git, Github and Eclipse on Windows

Having got github and eclipse talking to each other in OS X and Ubuntu I thought that doing the same in windows would be pretty straight forward. Unfortunately, I quickly ran into the mess that is ssh handling on windows. In the future I’ll post about getting git to work from cygwin and from the dos prompt, today I’m going to concentrate on getting eclipse to handle everything. This won’t get you the great command line but it will get you started. So let’s get started.

Install git eclipse plugin

The eclipse integration relies on the egit plugin. To read more about the project visit http://www.jgit.org/.
To install the plugin add the update site

http://www.jgit.org/update-site

Once you’ve added the update site open it and you’ll see that there are several builds available. I’ve been using the Release Build which seems to work well although the best option is likely to change. Check the box for the Release Build and hit install and follow the wizard.

Get started with github

ssh setup

Eclipse has it’s own setup for ssh key management.
Open up the ssh management in Preferences => General/Network Connections/SSH2.
Open the “Key Management” tab.
Click “Generate DSA key…”. This will generate your ssh key which is now shown to you in the ssh2 tab
Click “Save Private Key…”
Click “OK” to ignore the warning about saving without passphrase protection
Click “Save” with the default location to store the keys in your ssh directory
You now have ssh keys setup for use with eclipse
Copy the key into your clipboard from where it is displayed on the ssh2 tab, you’ll need this for the github signup.

Create account

Go to http://github.com/plans and signup to whichever plan you want. On the signup screen there is an area to add your ssh key. You should have this in your clipboard buffer from the previous step. You can now paste it into the github signup form.
Once you’ve finished the signup screen you’ll be logged in and taken to your github home.

Create repository

On the github home screen, next to “Your Repositories” click “create a new one”
Once you’ve filled the form out hit “Create Repository” and you’ll be taken to a page with some initial instructions. Follow these and you will end up with a local repository that is pointing to github as the origin.

You’re now setup with a github account and your first repository. So all that’s left is the eclipse setup.

Checkout from github

The egit plugin assumes that you haven’t checked out the project yet so if earlier you checked out your new repository into your workspace folder you need to delete it so that egit can check it out itself and manage it. If anyone has found a better way of doing this please let me know.

From the “File” menu select “Import”
Select Git/Git Repository
You now need the git address for the repository you created in github.
To get this visit http://github.com
Click on your repository under “Your Repositories”
Copy Your Clone URL:
Back in eclipse paste into the import wizard
Change protocol to git+ssh
Hit next three times and eclipse download your repository
You’ll then arrive at the Import Projects screen. At this stage your project has already been checked out onto the filesystem at path/to/workspace/project_name.
If you repository is currently empty then the list of projects will be blank
Egit will only checkout projects that have been previously been set up in eclipse
To remedy this we need to manually add a .project file to the root of your project
Using your favourite text editor create a file at path/to/workspace/project_name/.project

Paste in the following - changing the name to the name of your project

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

Once you’ve saved this return to eclipse and in the import wizard hit back and then next. When the projects screen is shown you should now see your project listed.
Hit finish and you should now see your project checked out in your workspace

OK, you’re now ready to start using your git repository from inside eclipse. For now there’s no command line git setup, my next two posts will be about getting git setup with cygwin and with the dos prompt.

Getting started with Git, Github and eclipse plugin on OS X and Ubuntu

Despite all the elements being in place for git, github and eclipse to all work together well I couldn’t find any information on how to make it happen. I’ve been setting up a project on github for use at work so I needed to get all of the developers on the team up and running. I’ve now got git, github and eclipse working together on OS X, Ubuntu and windows. This post is about getting going on OS X and Ubuntu (any debian based distro should be the same). The windows setup is a bit more involved (think ssh hell) so I’m going to leave that for another post.

Install git

OS X

Depending on what you want there are two options, git on it’s own or git with svn support and bash completion. If you’re lucky enough to be just using git repositories and don’t care about bash completion then take option one, otherwise option two is probably what you want.

1) Git only

Download the latest dmg from http://code.google.com/p/git-osx-installer/downloads/list
Open dmg and run the installer
Complete installer using defaults
Check that it’s installed correctly opening a terminal and

git --version

2) Git +svn +bash completion

This method uses macports so you will need to install that first. The instructions to do this can be found at http://www.macports.org/install.php

Once you have ports installed type

sudo port install -u git-core +svn +bash_completion

Check that it’s installed with

git --version

Ubuntu

Ah ubuntu, it’s

sudo apt-get install git-svn

Check your install with

git --version

Signup with github

ssh setup

If you haven’t got ssh keys setup you’ll need to do that now. In a terminal

ssh-keygen -t dsa

Hit return for each option and you will end up with both private and public keys in your ~/.ssh directory

Create account

Go to http://github.com/plans and signup to whichever plan you want. On the signup screen there is an area to add your ssh key. To get this, in a terminal

cat ~/.ssh/id_dsa.pub

Once you’ve finished the signup screen you’ll be logged in and taken to your github home.

Create a repository

On the github home screen, next to “Your Repositories” click “create a new one”
Once you’ve filled the form out hit “Create Repository” and you’ll be taken to a page with some initial instructions. Follow these and you will end up with a local repository that is pointing to github as the origin.

You’re now setup with a github account and your first repository. So all that’s left is the eclipse setup.

Integrate with eclipse

Install plugin

The eclipse integration relies on the egit plugin. To read more about the project visit http://www.jgit.org/.
To install the plugin add the update site

http://www.jgit.org/update-site

Once you’ve added the update site open it and you’ll see that there are several builds available. I’ve been using the Release Build which seems to work well although the best option is likely to change. Check the box for the Integration Build and hit install and follow the wizard.

Checkout from github

The egit plugin assumes that you haven’t checked out the project yet so if earlier you checked out your new repository into your workspace folder you need to delete it so that egit can check it out itself and manage it. If anyone has found a better way of doing this please let me know.

From the “File” menu select “Import”
Select Git/Git Repository
You now need the git address for the repository you created in github.
To get this visit http://github.com
Click on your repository under “Your Repositories”
Copy Your Clone URL:
Back in eclipse paste into the import wizard
Change protocol to git+ssh
Hit next three times and eclipse download your repository
You’ll then arrive at the Import Projects screen
If you repository is currently empty then the list of projects will be blank
Egit will only checkout projects that have been previously been set up in eclipse
To remedy this we need to manually add a .project file to the root of your project
In a terminal

cd path/to/your/workspace/project
vim .project

Now paste in the following - changing the name to the name of your project

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

Now save and exit, type
esc :wq return

Back in eclipse, in the import wizard, hit back and then next
You should return to the same Projects screen but this time your project will be available to select
Hit finish and you should now see your project checked out in your workspace

So there you go, git, github and eclipse all working in perfect harmony. Next up, using git-svn for all those pesky projects that are still in svn repos.