Friday, March 6, 2009

how to set up your own git repository with only a server, ssh, and git

This assumes you have ssh access to a server somewhere which you want to use as a sort of centralized repository and that server and your local machine have git on them. Don't tell anyone I used centralized and git in the same sentence, but centralized makes a lot of sense to me for lots of things, especially my own projects.

I'll do it the long way first and then with a tiny bit of bash scripting:

# ssh into your server
# on remote machine:
mkdir git
mkdir git/project1.git && cd git/project1.git
git --bare init

# on local machine
mkdir project1 && cd project1  # if you don't already have a project
# make a commit (you need at least one commit)
git init
echo "some text" > file.txt
git add .
git commit -m 'init commit'

# now we will push it to the server
git push ssh:// master

# we still need to link our git folder with the server
git remote add origin ssh://
git config branch.master.remote origin
git config branch.master.merge refs/heads/master

Now for the BASH scripting I promised. This is basically the same thing but maybe a little easier to copy and paste once you set up your variables:


# set up the remote git repo in one command:
ssh $SERVER "mkdir git/${PROJ}.git; cd git/${PROJ}.git; git --bare init"

# (inside your project directory... with files/dirs already present):
git init ; git add . ; git commit -m 'init commit'; git push $PROJ_URL master ; git remote add origin $PROJ_URL ; git config branch.master.remote origin ; git config branch.master.merge refs/heads/master

And my rant...

At least in the ruby and linux developer community, Git seems to be quite hip right now. I personally think bazaar wipes git all over the floor, but due to server circumstances beyond my control I need to version control my files with git.

While the above operation is CLEARLY and SUCCINCTLY documented in the main bazaar docs, I've had to piece this together myself for git. Now, I suppose if I had actually read and digested the volumes of arcane documentation, switches, and nuances of git, I wouldn't be griping right now. That said, projects like 'eg' can make it easier and I'm sure the things people want to do with a vcs will become documented soon enough in git... the beauties of open source.


alfredh said...

I think 'git init' is missing on the local client repos..

JTP said...

indeed, thanks alfredh. Should be fixed now.