November 23, 2008

Posted by John

Older: jQuery on Rails: Why Bother?

Newer: RubyGems: Yours, Mine and Ours

Git'n Your Shared Host On

GitHub is awesome, really awesome for open source projects and for projects with multiple people. In fact, if you use your own git setup for an open source project, you are most likely hindering your project’s progress and wasting your time.

On the other side of the coin, if you are a lone shark and you don’t need GitHub’s awesome social features, I have news for you: you can host your own git repositories really easily and on the cheap side.

Remote Setup

If you are like me, you probably have a DreamHost, TextDrive or some other cheap shared hosting account with ssh access. What you might not know is that is all you need to host your own private git repositories.

ssh username@yourcheaphost.com
mkdir -p ~/git/yourproject.git
cd ~/git/yourproject.git
git --bare init

That is it. Your git repository is now setup. Not too hard eh? You could put that anywhere but a folder named git makes sense to me.

Local Setup

So your remote server is now setup, but how do you use it? Glad you asked. Open up a new tab (or window) or quit your ssh connection and cd to wherever you want to setup your project locally.

mkdir yourproject
cd yourproject
git init
git remote add origin ssh://username@yourcheaphost.com/~/git/yourproject.git
touch .gitignore
git add .
git commit -m "Initial Commit"
git push origin master

At this point you have now pushed to your remote repository and are almost good to go. The last thing is you need to add the following on your local machine to .git/config in your project.

[branch "master"]
  remote = origin
  merge = refs/heads/master

The End

That is it. You can now push and pull at will. If you want to give anyone else commit access, just add their ssh key to ~/.ssh/authorized_keys and you can work on the project with a friend. Setting up your own git repositories is really easy, as you can see, so don’t be afraid.

Further Reading

So what I showed above is really the most basic setup you can have. It involves pushing and pulling using ssh and is easy peasy. If you want more than that, such as finer grained access control, enjoy the following links.

Casper Fabricius wrote a handy shell script that allows doing all that I mentioned above on DreamHost in one easy terminal command.

Rick Olson has an article on moving to git where he shows a rake task he used and also links to gitosis.

Speaking of gitosis, Garry Dolly has an article on setting up gitosis and how to add users and manage their repository permissions.

Tim Lucas has a good post on setting up a new remote git repository.

GitHub has a ridiculous number of guides that are geared towards using GitHub but have morsels handy for non-GitHub usage.

Happy, cheap git hosting!

10 Comments

  1. Pardon my ignorance, but what would be the benefit of using Git over SVN? This may sound dumb, but besides the social features I am still trying to figure out the underlying benefit that would force me to switch completely.

    Thoughts?

  2. @Nate – sometimes it is hard to explain but I promise you git is a breath of fresh air. Probably the best thing about got is the way it branches and merges. In svn branching is such a pain that I never did it. With git, however, I branch all the time. One benefit of branching a lot is it allows you to start working on something new, but quickly jump back to your stable code base for a bug fix.

    Another benefit of git is speed. Git is really fast. When I go back to a project on svn it feels painfully slow checking out and in and also when deploying. Git is quick.

    The last thing I can think of is size. Git repos are really smart about size and are this relatively tiny.

    I didn’t really feel like I was missing anything in subversion but I am much happier with git. Heck, git’s svn commands are better than svn. That is kind of another killer feature. You don’t have to fully swich to git to try it out. Git works pretty good with svn repos.

  3. Oh, and my favorite thing about git that I somehow totally forgot to mention? It works offline. You can have no internets and commit away and when you get back online a simple push to the remote server and it is like you were never gone. :)

  4. I wrote a Capistrano task to do something similar. I can initialize a local directory with git, work for a while, then run a single command to copy it to my remote server.

    It uses sudo but could be modified to work on a shared host.

    http://gist.github.com/28541

  5. Kevin Marsh Kevin Marsh

    Nov 24, 2008

    Probably goes without saying, but you can push git repos to as many remote hosts as you’d like. You’d just have to use something like origin, origin_eu, origin_us_west, or something unique for each. Distributed backups!

  6. @Geoff – Nice! Good idea popping that in ~/.caprc

    @Kevin – Great point. The fact that git is distributed is also really cool.

  7. what is the —bare opinion work for?

  8. It creates a repository without checking out files. So no working directory.

  9. Thanks a lot for this article ! I needed some pieces of info about git and ssh to migrate my project to git.

    For Nate and those who wonder what the benefits of git are I posted this article: <http://harryseldon.thinkosphere.com/2008/11/08/grand-gardening-with-git >.
    To make it short, I find git amazingly useful when it comes about dealing with open source projects with lots of branches including your own branch.

  10. OK looks like it did not like the brackets. Sorry, here is the link.

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a web developer and programmer who has fallen deeply in love with Ruby. More about John.

Syndication

Feed IconRailsTips Articles - An assortment of howto's and thoughts on Ruby and Rails.

Feed IconRails Quick Tips - Ruby and Rails related links.