Q:

How can I cd outside my git hook directory

I'm writing my own git post-receive hook in ruby, and it resembles something like this:
if !File.exists?(rep_dir+repo)
     puts "Cloning repository #{repo} into #{rep_dir}."
     `cd #{rep_dir}; sudo git clone file:////home/git/repositories/#{repo}.git`
exit
end

This hook is setup in gitolite common hooks, so when I push configurations for a new repository, I clone it right away to a new location.
I also loop for each project in gitolite config to check if the repository exists for each project, but that's not where I'm having problems.
My issue is, whenever this script runs I get:
remote: cd: 1: can't cd to /home/<somedir>/repositories/
remote: Cloning repository gitolite-admin into /home/<somedir>/repositories/.

The repository was not cloned to the target directory after the hook ran. I read about unsetting the GIT_DIR environment variable, but I had no success.

A:

You don't have to cd anywhere when you are cloning.
You can simply add the destination path as a parameter to clone command. 
git clone file:///xxx /path/where/to/clone

(Make sure the destination path doesn't exist, or the git will refuse to clone there)

