Moron no more : I can Git !

Since Linus said we’re all morons ’cause we use Subversion and the like, lots of people started to migrate to his new faster, better, stronger, cooler and geekier revision control system Git.

Moron

Installation for Subversion morons like me

Get the latest stable source from http://git.or.cz/

wget http://kernel.org/pub/software/scm/git/git-1.5.3.7.tar.gz
tar xzf git-1.5.3.7.tar.gz
cd git-1.5.3.7.tar.gz
make prefix=/usr/local all
sudo make prefix=/usr/local install

Create your own little repo

mkdir moron
cd moron
git init

Congrats, you just created your first Git repo!

Most common commands looks like Subversion ones. add, rm, mv and commit. But you gotta remember that there’s no such thing as a working copy in Git. It’s a distributed revision control system. Which means that your local copy is a repository. In fact, with Git you do not checkout (in the Subversion sense) a copy of the repo, you clone it. That’s why Git is so much faster. Most of the commands: history browsing, local commits, reverting, etc, are done locally, without talking to the server. Check the official guide for Subversion morons.

Try adding a file:

echo "I'm no longer a moron Linus" > moron-no-more.txt
git commit -a

The -a ensures that all new files are added, you can do this manually with the git add command.
You then commit you change as you would do in Subversion, with the difference that it’s all done locally:

git commit -m "Take that Linus!"

All is not lost for us poor Subversion mortals

The cool thing about Git is it has builtin support for playing nicely with Subversion. Which ease the migration and can even let you use Git side by side with Subversion. So you can import a Subversion branch like this:

git svn init http://repo/url/trunk
git svn fetch

You Git repo now has all the revisions of you Subversion branch. Try running git log.
You can even send you changes back to Subversion:

git svn dcommit

Create your central repo

mkdir mah-repow.git
cd mah-repow.git
git --bare init-db
git-repo-config remotes.origin.url http://yourhostname.com/git/mah-repow.git/; # notice trailing slash!
git-update-server-info

Upload that directory to your public server. Like Subversion, Git supports various ways of serving a remote repo, I chose WebDAV because it’s the simpler one on DreamHost.

To send your changes to the server with Git you push your changes to the server and pull changes from others. With our previous examples, to push a series of commits to the server you’d do:

git repo-config remotes.origin.url http://yourhostname.com/git/mah-repow.git/; # once to setup the default server url
git push

Happy Gitting!

About these ads

17 Comments

Filed under tips, tutorial

17 responses to “Moron no more : I can Git !

  1. Git is indeed awesome. The only thing to keep in mind is that the tooling for rails deployment with git is still a little rough, at least last I checked (a few months ago). Took me a day of fiddling with capistrano and google before I finally got something going.

  2. It looks pretty interesting, but I’m still unsure of which side i am standing in this “Local copy Vs Cloned repository” thing. One cool thing about svn (at least for me) is that your changes are “safe” whenever you “commit” them. But if you commit locally, this advantage is gone. Maybe it looks more problematic that it really is though… and since i didn’t try it yet I cannot really say if I like it or not.

    As for you Marc, do you prefer Git to Svn? And if it’s the case, do you intend to switch to Git “for good” ?

  3. I was following Djief’s articles on Git but I think you’ve done a great job at distilling the same topic to something even more easy to digest.

  4. FrankLamontagne: The thing about Git is that it allows you to set up any number of remote repositories, and push/pull from them as you see fit. If you want to use it as you would Subversion (one central repository which everyone commits/pushes to), that’s totally possible. In that scenario, you’d still get the following advantages from using Git:

    1. Speed: no need to go over the network to query the repository.
    2. Data security: have a complete copy of the repository on everyone’s workstation, instead of just in one central place.

  5. @William: I haven’t used Git w/ Rails yet, only on a Ruby gem project. Did you find any lib to use Git w/ capistrano ? Else then Cap deployment what were your problems?

    @frank: Indeed you commit locally but you can push your changes to a public server as often as you want. And I think it’s safer since your repository is gonna be everywhere it’s used (server + all clones). If your server go down the next push will bring everything up to date since you _clone_ the repo. I don’t think a full switch to Git is the best thing now since a lot of the good tools depend on Subversion (William pointed Capistrano amongst others). But replicating to Subversion is so easy w/ Git it’s gonna be a slow transition I guess.

    @chris: I don’t know how I missed that, I swear JF blog was on my Reader but it mysteriously disappeared. Very nice posts!

    thx for the comments all

  6. Zelnox

    I’m in the middle of baking a cheesecake, but I did watch the beginning of the video. Linus is funny ^_^

    Git sounds interesting, but I wonder if it is overkill for smaller projects and/or smaller teams. Still, I do like the ease with which branching and merging is done. Working offline? That beneficial?

  7. Hey Vinh,

    It sure is overkill now, most dev know svn but not Git and we can live all right with svn on most project now, since we don’t have a codebase like the Linux kernel ans thousand of patches to merge. But I think it’s a good tool to start playing with. I’m starting to like the speed of it and I’m feeling it’s gonna be the de-facto standard in a couple of years.

    Yay, cheesecake, hmmm!

  8. @macournoyer: Wrt Cap and Git, I used this:

    http://scie.nti.st/2007/9/4/capistrano-2-0-with-git-shared-repository

    If I recall correctly, there was some minor twiddling involved. If it doesn’t work out of the box, feel free to poke me and I’ll run a diff against his code. :)

  9. Darius Damalakas

    One thing git is superior (note: personal opinion here!) to subversion is the very philosophy of it:
    git is decentralized system, svn is centralized.

    De/Centralization I consider to be a very important factor as a whole, not limiting only to computers, etc. Compare ftp torrents. centralized network (phone) and decentralized network (internet). Centralized government with decentralized.

    Decentralized solutions are more robust under certain circumstances, since they do not need the “center” to orchestrate things.

    Now i don’t say we will migrate from centralized repository to decentralized, there are good points in both cases. Just need to be aware of both solutions pros and cons.

    Nice article!

  10. I agree Darius. But with small project with only 1 or maybe 2-3 devs de/centralization doesn’t make that much of a difference I think. But for big opensource projects I think it’s a must!

    thx for the comment!

  11. Capistrano 2.1 supports Git natively.

    Capistrano 2.0 can use this: http://scie.nti.st/2007/9/4/capistrano-2-0-with-git-shared-repository

    Capistrano 1.x can use this:
    http://scie.nti.st/2007/3/16/capistrano-with-git-shared-repository

    So really, no more of this you can’t use Git w/ Cap talk ;)

  12. wow!! I didn’t know cap 2.1 had this builtin, thx for the links garry! I promise, no more of this kinda talk here ;)

  13. More than that. If you really don’t want to go all Git for now, you can still use Subversion as your main repository and rely on git-svn. That way you gain the best of both worlds: for now you can rely on everything you know about Svn maintenance and deployment procedures already in place, and in the workstation you gain the best and most reliable way to develop while disconnected from the network. I am using git-svn for a Rails plugin development and I am using Google Code’s svn for that, but using git locally and it works seamlessly.

  14. bayareaguy

    Git sounds interesting, but how do you do the equivalent of svn commit hooks with it?

  15. @Akita: yeah git-svn is definitely one of the coolest feature of git right now, since it ease the migration.

    @bayareaguy: there’s a .git/hooks dir in each Git repo

  16. Get reliable bug tested casino script and software for your Online Casino at casinowebscripts.com. Find best deals and lowest prices for online gambling scripts and software here!!

  17. There are some fascinating points on this article but I don’t know if I see all of them heart to heart. There’s some validity but I will take maintain an opinion till I look into it further. Good article , thanks and we would like more! Added to FeedBurner as well.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s