Lullabot Ideas
We know stuff. We empower you to know stuff too.
Making the transition to Git
Blog by Jerad BitnerMay 10, 2010 - 10:33am
So you've probably already heard that Drupal.org is turning to Git for it's version control system (VCS) needs, but you may be wondering, "Well, how do I get into the practice of using Git?". And if, like most developers, you are using Subversion for most of your projects, then I have a really great suggestion on how to start making this transition.
Background
Subversion impaired?
Subversion impaired?
I recently read an article that stated that if you are used to using Subversion for your VCS needs, that you are basically brain damaged! It's a play on an article that compares Mercurial to Subversion, and the concept is really quite similar, being that Git and Mercurial are both distributed VCS systems, while Subversion is centralized. While I'm not sure I would go as far to say you are 'brain damaged', it really is a fundamental shift in thinking.
A basic but imperative concept
The very basic concept you must understand in the difference between Subversion and Git, is that a Subversion repository is hosted remotely and you just have a checkout of the files that are in that repository on your local machine. With Git, you actually get the whole repository locally (hence the term 'clone') and then you also have a checkout of the files that are in that local repository.
Here is a tool to help you transition.
Git-svn
Most development shops use Subversion for their VCS needs. It's not really going anywhere soon, and the transition to a Git world will probably be a little slow, especially if you are collaborating with other shops or people who do not know Git, are unwilling to learn Git, or you just don't have the time/resources to teach or convince them of Git. So if all of your repos belong to SVN, how can you get into the practice of using Git in your daily work life? In steps git-svn. This great tool allows you to work with a remote Subversion repository while using Git on your local machine! You can run all of your normal Git commands locally, merging, branching and even merging Subversion branches (which Git is far superior at doing) all without using Subversion commands and still keeping your remote Subversion repository intact. Other developers can still use it exactly how they are used to, and run Subversion commands if they want... but you will have the real power!
Installation
Installation is pretty simple, but the guys over at MetalToad have already got that covered, as well as some basic commands and cheatsheets, so I'll not go into further detail here.
Typical workflow
My typical workflow when having to collaborate with another team who is using Subversion is as follows:
- Checkout the code.
- Make your code changes.
- Commit your code (purely through Git).
- Check the repository for changes.
- Push your code to the remote repository.
$git svn clone <repository-address>
This pulls down the Subversion repository just like $svn checkout would and then puts it into a Git repository locally, adding the original as a remote branch so that you can still push your code back into the original Subversion repository.
$git commit -am "typical commit message"
This is basically, 'Save'. It commits your changes to your repository, which with Git, is actually on your machine (a main difference between svn and git, as I mentioned). You have not changed anything in the original Subversion repository at this point, everything is still local.
$git svn fetch
$git svn rebase
These two commands are basically the equivalent to doing a $svn up. The first command pulls down any changes that are in the repository, and the second command rewinds any changes you made, applies the new ones it just got from the repository, and then replays your work on top of those changes.
$git svn dcommit
For all to all intents and purposes, this command is the basic equivalent to $svn commit -m "typical commit message". It actually does a bit more than that.
From the documentation:
Commit each diff from a specified head directly to the SVN repository, and then rebase or reset (depending on whether or not there is a diff between SVN and head). This will create a revision in SVN for each commit in git. It is recommended that you run git svn fetch and rebase (not pull or merge) your commits against the latest changes in the SVN repository. An optional revision or branch argument may be specified, and causes git svn to do all work on that revision/branch instead of HEAD. This is advantageous over set-tree (below) because it produces cleaner, more linear history.
Conclusion
Git is coming. It's better than you can imagine, and with the imminent approach of Git coming to Drupal, you can finally have everything in one VCS. This is a way to start edging into it, using it in your daily life if you use Subversion regularly now. And CVS? A thing of the past... barely worth mentioning. I for one welcome our new Git overlords!

Comments
One major problem with this
One major problem with this is that you cannot have git branches.
Everytime you merge something from a git branch into "master" you will have to rebranch master to make it working.
git-svn leads to some extra work compared to git.
Just tried it out
I'm a drupal developer, we use svn at work which often is troublesome. I tried git and learned the basics a few weeks ago and I am very impressed, the speed is amazing and branching, tagging etc. is really easy. I'm really looking forward to use git in a production environment with other people. Only thing though is that I miss a good gui for mac. Or maybe I will learn to get comfortable with the command line :)
GitX and Gity
Hey Didrik! Check out GitX and Gity. Both are open source and good in different ways. Ideally they would be one app, though they work fine side by side.
But seriously, somebody should merge the two apps and call it GitZ.
Thanks for this article. As
Thanks for this article. As someone who's used SVN and CVS for years that post by Metaltoad from a few days ago was still complete gibberish to me. Your article makes things clear enough that I actually feel like I can try it out.
Awesome
That was certainly the goal! :)
Need enlightenment
Ok. So everyone has their own repository with git. Let's say you have a team of 5 developers working on a common codebase. Now if all 5 people are coding away, you've got 5 repositories that need to be synced with another? To me, that seems to be a pain in the ass. Obviously, I'm missing something ...
What about when you want to release a build of your project? Which repository do you use? Or do you set up a 'master' repository that all the others push their changes to. In which case, isn't that just the same as having a master Subversion repository? Sure, all your devs get quick local commits w/their own repos, so I can see an advantage there. But syncing your repo against multiple coworker repos just seems like extra work to me.
(note: I'm not bashing git, I don't understand the appeal and need help switching on the lightbulb :)
Enlightenment
Typically I'm using a 'master' repository, and in the case of how Drupal.org is going to use Git - they too will have a central repository for everything that will house the latest and greatest. So yes, it is basically the same workflow as having a master Subversion repository.
Advantage?
So then, if the workflow is the same as Subversion, where is the advantage in using Git? The only thing I can think of is that having your own local repository reduces the need to create branches.
Is that it?
Lots on this
There are a lot of articles you can find on the web with some very good reasons outlined, but here is one that I find to be pretty succinct: http://markmcb.com/2008/10/18/3-reasons-to-switch-to-git-from-subversion/
This answer on StackOverflow is also really good: http://stackoverflow.com/questions/871/why-is-git-better-than-subversion
tracking subversion branches as git branches
So I found a great way to do merging using git-svn. When you do your "git svn clone" command, you can add a few options so that you can track the trunk and differentiate it from your branches. This means that if your other developers are using branches in subversion, you can easily merge those changes into trunk or keep your branch up to date with trunk.
$ git svn clone [svn repo location] -T trunk -b branches -t tagsAfter this is done cloning, you can run
$ git branch -rin order to see the subversion branches as git branches.Now you can switch between these branches in git just like git native cheap and light local branches and work with them as you would your regular git branches.
This is pretty important. If you've ever tried merging in subversion you can see how valuable this is. The documentation is confusing and it even says merging is a headache. But with git as as simple as:
#switch your branch
$ git checkout [branch-name]
#merge in your changes
$git merge [other-branch-name]
More specifics here: http://www.jukie.net/bart/blog/svn-branches-in-git