How to initialize a remote Git repository

Well, since it took me all day to figure this out again, I figured I might as well write it down somewhere so that at least I’ll be able to remember it the next time I want to create a new repo with Git.

The one thing that irks me is that I always try to create a bare repo on the server and then clone it to my client.  For some reason git doesn’t want to work this way even though that’s what my intuition tells me.

Anyway, the way to do it is to first create your working copy.  I’m making a little website so I have some code in /www/my-working-copy.  After you have some code that you want in the repo, you need to initialize a git repository and add your code to it like so:

git init
git add .

After that’s done, you want to go your git-daemon’s base path and clone this repo there with a bare copy:

cd /my-git-daemon-base-path && git clone –bare /www/my-working-copy my-remote-repo

You should get a message like “Initialized empty Git repository in /my-git-daemon-base-path/my-remote-repo/” after which you may need to run update-server-info and you’ll also want to add a ‘git-daemon-export-ok’ file to the repo so that the daemon knows that it is okay to export it:

git update-server-info
cd /my-git-daemon-base-path/my-remote-repo && touch git-daemon-export-ok

At this point you also will most likely want to add a ‘tracked repository’ named ‘origin’ to your working copy so that git will know where you want to push and pull from:

cd /www/my-working-copy && git remote add origin /my-git-daemon-base-path/my-remote-repo

That’s it!  To test it out simply try a commit:

git commit -a

You should receive a message “nothing to commit (working directory clean)”.

At this point you probably want to push your changes to the repo.  Although the repo is created, it won’t be very useful until there’s something in it:

git push origin master

Cheers!

Tags: , , , ,

Leave a Reply