My Octopress Blog

A blogging framework for hackers.

3 Reasons I Use Git Instead of SVN

This was a response to an email from a friend about why he should switch to Git from SVN. Perhaps you’ll find it useful as well. :)

———-

Three things come to mind right away:

1) Distributed instead of centralized.

With SVN, there is one “true” copy of the code at all times. Everyone checks out the code from this one true copy and checks the code back in when you’re done. There is nothing wrong with this, and in fact, this is also what you will also usually have with Git. The big difference is that you don’t *HAVE* to have it with Git if you don’t want to.

For example, with OnceOvers, Dan and I work off of the development installation that is up on Heroku. This acts as the “true” copy right now, meaning that Dan and I both check out  a complete version from the one on the server and push code back into it. This works great for now and is essentially the same as what you would have with SVN.

However, as soon as we are ready to take the site live, I will be pushing the site to a production installation hosted somewhere else. This is now the production instance of our website, instead of the development one. The map looks something like this: 

So that means that Dan and I can keep pushing and pulling from the development server without worry. When we want to take whatever we have live, we just push the current state to the production server. Additionally, I can also keep another copy of it on GitHub (or any git server) with only a single command.

Also, because of the distributed architecture, I can keep pieces of the code in all sorts of places. So, if I wanted to OpenSource a portion of the application but keep another portion of it closed, that’s super easy.

The distributed piece is also interesting because you don’t need an internet connection to check in some changes. The advantage to checking in changes is that you can see where they were and roll back to that point in the future. (Kind of a like a checkpoint in Mario) If you are working in SVN, you need to commit the changes to the server before breaking the whole page again or else you might lose your progress. With git, you can check in to the repository on your laptop as many times as you want without needing to connect to the internet, so you can wipe the whole file and be right back where you were within seconds. When you do get to an internet connection, you can just “git push” it to the remote server.

Speaking of the remote server, because both copies of the repository are full copies, you don’t have to worry about losing the repository if the hard drive on the server crashes. You just specify a new remote repo and you’re back up and running in minutes with all of the history still intact. I’ve done this a number of times just when moving to a new hosting provider.

2) Super easy to create branches of your work to try new things

Now, all this is fine and dandy and not really that compelling since most of it can be done with SVN. However, where it gets really powerful is branching and merging. 

As a programmer, even when I’m working with HTML / CSS, I might try some crazy new idea that involves editing a bunch of files even though I’m not sure it’s going to work. With SVN, because I’m working from a server copy, I would either have to checkout the entire repo into another folder and do all my craziness or I would have to start working on it and change all of my files knowing that it would be a pain in the but to go back to the beginning. With SVN this also restricts me from making little changes while in the middle of a big change.

Lets say you’re working on a site for a client and you want to move the entire HTML / CSS structure over to HTML5 / Eric Meyer Reset from XHTML 4.1 / YUI Reset. You start work and get about 3/4 done when the client calls and says there is a typo on the live site that needs to be fixed immediately. How do you go back to the live copy and make the little change when you’re in the middle of a big change? With SVN, you would checkout a new version of the repo, make the change and check it back in. Here are three ways to do it with Git, in order of complexity.

  1. “git stash” will literally stash all changes that you have made and revert the files back to their last commit state, as if you had just downloaded them. You can make the little change, check it in and push it to the server. When you’re ready to get back to work, you just type “git stash apply” and you are back to where you were. With SVN you can do this with a diff / patch workflow, but it’s a bit more cumbersome than just typing stash. (Thanks @snookca for the reminder)
  2. “git add -p filewithtypo.html” will send you into interactive / partial mode. If there are multiple edits in a file but you just want to check in one of them, this will allow you to pick which one and check it in, instead of checking in the whole file. It’s awesome. :)
  3. Branching. With SVN, branching was kind of a pain in the ass. A bunch of smart people who use SVN seem to think it was easy enough, but I never really used it because it always broke or screwed something up. I guess I just wasn’t smart enough. Anyway, branching and merging in git is really straight forward. Lets say you want to work on the HTML5 rewrite. You can run “git branch html5” and it will create a new space for you to work. To work in that space, just type “git checkout html5” and you are now in the html5 branch. You start making your changes and then get the call from the client about the typo. All you do is type “git checkout master” and you’re back in the clean branch pre-changes. You fix the typo, check it in, push it to the server and then type “git checkout html5” to get back to work. When you are done making all of the HTML5 changes, you can run “git checkout master” to get back to the clean branch and then type “git merge html5” to apply all the changes that you made. It’s that easy. This also lets you work on totally separate features at the same time without any conflicts since you can have as many branches as you want. No pushing or pulling necessary to switch. :)

3) Cleaner and smaller folder sizes

With SVN, you get all those annoying .svn folders created in your folder structure. If one of the folders get messed up, you’re in trouble. The folders hold the “clean” copy of every file so that you can revert back to it whenever you want. If you have made changes and then somehow screw up the .svn folder, you have to do a diff of your changes, svn up and the patch the files back together. Kind of a pain in the butt. That also means that every file you checkout is duplicated.

With Git, there is only one folder created at the root of your project directory called “.git” and it holds all of the information about the repo. Instead of storing full copies of all the files, it stores diffs from one version to another. So, it only stores records of the changes. This lets you literally “play back” the changes that you’ve made over a number of commits. This is why it is so much easier to branch and merge files. Since diff files can be fuzzy, you don’t have to compare exact copies every times which results in less collisions. Also, the diffs are generally much smaller in size than an actual file, so you save space.

That about sums it up. As for why you would use this instead of SVN, that choice is totally up to you and SVN is still a darn good version control system.

Versions is probably the best OSX visual version control application out there, although Tower is making progress on providing a Git one ( http://www.git-tower.com/ ) and Gitbox is already fairly mature ( http://www.gitboxapp.com/ ). As for Kaleidoscope, it works with Git as well and ties into Gitbox already.

Personally, I like using the command line. I just find it so easy that I haven’t found a reason not to use it yet.

I hope that answers some of your questions. :)

The 21 Best New-ish Songs That I Don’t Think Enough People Have Heard

The other day, I was hanging out with some co-workers of mine and we started talking about music. It turns out the neither of them had heard many of the songs that I was really into lately. I promised to introduce one of them to my musical tastes, and went home to make a CD.

I then started playing the CD for a few other friends, and it turned out that NONE of them had heard most of the tracks on the CD. Had they not all really liked the CD, I would have just written myself as having odd taste in music. But since they did, I’m writing this post to make sure everyone else can be exposed to the more awesome music.

Another thing that I realized after looking over the album is that almost all of the songs were introduced to me by Morgan Holzer. That girl has wicked taste in music and I highly recommend you check out her PopTen stuff

  1. Sweet Disposition - The Temper Trap
  2. Animal - Miike Snow
  3. Heart Skipped A Beat - The xx
  4. Kiss With A Fist - Florence and the Machine
  5. Little Lion Man - Mumford & Sons
  6. Home - Edward Sharpe and the Magnetic Zeros
  7. Sweet Sixteen - Think About Life
  8. 28 - Nano Whitman
  9. Dance Song - Orchard
  10. Punctual As Usual - The Parson Red Heads
  11. Man of a Thousand Faces - Regina Spektor
  12. Fader - The Temper Trap
  13. The Great Defector - Bell X1
  14. Dying Is Fine - Ra Ra Riot
  15. Bang! Bang! - The Knux
  16. Wish I Stayed - Ellie Goulding
  17. I’ll Kill Her - SoKo
  18. Paris - Friendly Fires
  19. Edward Is Dedward - Emmy The Great
  20. Why Do You Let Me Stay Here? - She & Him
  21. Untouched - The Veronicas

Also, please don’t just download the songs. All of these artists are hardworking and deserve your dollars. You should be able to find most of them on iTunes. :)

    Rails 3 - Removing or Adding Www in the Url

    I don’t like having two separate domains that point to the same content for a number of reasons. Caching and SEO optimization are two them that make it worth dealing with for me. Here is how I deal with that in Rails 3.

    Usually, I would handle this in my webserver config file but since I’ve started using Heroku I don’t have access to a config file so the Rails application needs to handle that for me.

    One way to do the redirect is to have some code in your application_controller.rb file that checks for www. and then behaves accordingly. Like this:

    before_filter :strip_www
    
    def strip_www
      if request.env["HTTP_HOST"] == "myapp.com"
        redirect_to "http://www.myapp.com/"
      end
    end

    This functions just fine, however, there is no need to load the entire application stack just to do a redirection.

    Fortunately, Rails runs on Rack which means you can run some pretty awesome middleware and a few of the guys over at pivotal labs have done just that.

    Enter Refraction.

    Refraction is a piece of middleware that gives you a router before the request reaches your main application stack, which means you can intercept the call and perform a redirect on it without loading a bunch of unnecessary code. You can read a lot more about it in the README file on GitHub.

    All I’m going to show you here is how to ADD “www.” to a request that doesn’t have it. These instructions are fairly similar to the README.md, with the exception of the refraction_rules and a small change to the production.rb call.

    First, install the refraction gem. I recommend adding “gem ‘refraction’” to your Gemfile and running “bundle install” in your app. This will also install it on Heroku when you push.

    Once you have done that, we are going to add the following call near the top of your config/environments/production.rb file:

    config.middleware.insert_before(::Rack::Lock, ::Refraction)

    and then create a new file inside the config/initializers/ folder called refraction_rules.rb that has the following content in it:

    Refraction.configure do |req|
      req.permanent! :host => "www.mydomain.com" if req.host !~ /^www./
    end

    Once that’s done, you should be good to go. I tested this on my dev box by setting my /etc/hosts file up to point www.mydomain.com and mydomain.com to 127.0.0.1 which should work nicely for testing. :)

    Good luck!

    Yes, I’m Changing My Name to Jeremy Baker

    Summary: After five years of deliberation, I have finally decided to change my name from Jeremy Baker Hubert to Jeremy Hubert Baker. Tara is so awesome that she will be taking the Baker last name as well. :)

    Some history of my father and the Hubert name

    As a few people know, my dad was adopted and was raised by the Hubert family in Chevy Chase, Maryland. He was given the name Stephen Fitch Hubert by his adopted family. He spent years of his life trying to track down his birth parents with no luck, however, after he was diagnosed with Lou Gherig’s Disease the US Government finally released their names. It turns out, his birth name was Stephen Mark Gray. He changed his name in an attempt to reconnect with his birth name, and when he passed away he was legally known as Stephen Mark Grey. (Yes, he misspelled it)

    In the middle of all that was his life and during that time, among other things, he managed to get married and have two children.  My mom, being the wonderfully independant woman she is, kept her maiden name. He stuck around for about 6 years, but after he left I was the only Hubert guy in the area.

    Although I met Mrs Hubert a few times and she was very kind to my mother, she passed away when I was young. Mr Hubert had died of polio before I was even born so I never had much connection with him. The only real connection to the Hubert family now lies in my uncle, Larry Hubert, who is a wonderful man and lives in Maryland. I see him every now and then, but really not often enough.

    Some history about the Baker family

    All of my life, I have been Nancy Baker’s son. She did a fantastic job of raising my sister and me and has shown a tremendous amount of strength and integrity. Further down the line, Bert Baker is my grandfather and Jackie Baker was my grandmother. I have a direct connection to the Baker family and I am proud of their heritage and history. My grandfather has worked hard all of his life, and has done an amazing job of providing for his family. His father was a Sea Captain and another hard worker. The Baker lineage is strong and I am proud of their past and present… but what about their future?

    There is only one male descendent of Bert and Jackie Baker with their last name. The rest of them are either women, or have the last name from the man my aunt married. 

    Why am I changing my name…

    I want to carry on the name of my mother and my grandparents. I want my children to have their name and be able to look back on their last name with pride. I admit, having a name that’s easier to pronounce is appealing as well and, as Tara points out, our kids will be at the front of the line. :)

    My mother seemed to feel the same way when she named me. Instead of giving me some typical middle name like Matthew, she stuck her last name in there as my middle name. It has always been rather strange for me, going through life with a last name sandwiched between two first names. (ie: Hubert Humphrey) 

    Thus, it makes the decision just a bit easier. Instead of drastic name change, I’m just going to flip my last and middle names to become Jeremy Hubert Baker

    Let the complications begin!

    In the old days (pre internet), changing your name was relatively easy. You could literally just start going by a new name and that was the end of it. Perhaps you would have to file some paperwork at the Bank so that they would recognize cheques made out to you.

    That definitely isn’t the case anymore.

    The process to change your name is now a legal one that takes more than three months and costs a few hundred dollars. As if to demonstrate how dated and surreal the whole thing is, you even have to put an ad in the local paper letting everyone know that you will be changing your name; as if ANYONE that knows me reads the local San Francisco newspaper.

    And there there is the internet!

    These days usernames, email addresses, domain names,  and other identifiers really complicate things. In my case, I have been on the internet since about 1995 getting emails, building websites, joining networks and developing a brand. I take up 9 of the 10 Google results for the name Jeremy Hubert. I am jhubert at gmail and jhubert on pretty much every social network out there. I have the jeremyhubert Yahoo! IM address and own JeremyHubert.com and jhubert.com. I was early to this whole internet thing and got a headstart. Now I want to change.

    15 years into the world wide web, I’m now a late bloomer. jbaker everything is taken. jeremybaker everything is taken. There are a *few* jeremyhbakers left. I suppose I have to hope for an overhaul of the internet in order to get that all sorted out. :p

    What about my lovely wife?

    Well before the wedding, I had told her my dilemma and she was happy to take whatever name I chose. However, as the wedding got closer and I still hadn’t made up my mind, we had to put something on the invitations. Because of the timeframe required to change your name, I made the call and settled on Hubert. On September 4th, Tara Lynn Purtle became Tara Lynn Hubert. At least on the wedding certificate. 

    Being the loving wife she is, she has agreed to go along with my craziness and to change her name at the same time as me so that we will both be Bakers. Personally, I think Tara Baker is a pretty cute name for her, so I’m excited about that.

    We have also joked about her just becoming “Tara” and dropping the last name all together, a la Madonna or Cher.

    In conclusion…

    I will be taking notes as I try to transition over from Jeremy Hubert to Jeremy Baker. It is going to take a LONG time, I’m sure. I may keep Jeremy Hubert as my online presence and just use Jeremy Baker in everything formal and every day life.

    Whichever one, don’t worry. I’m not going to get mad if you call me Jeremy Hubert. I’m not going to correct you or ignore you until you say the “right” name.

    After all, a rose by any other name?

    OnceOvers.com

    This site helps people raise money.