Git essentials for SVN users

Coming from Subversion, the extra steps involved in working with a Git repository can be confusing. Here are some basic concepts and commands to get started with Git.

I find that actually doing something is the best way for me to understand. If you’re the same way, the Github tutorial by Code School is great.

Concepts

There are a couple conceptual differences between Git and Subversion that it helps to understand before getting started.

Centralized vs Distributed

Subversion is centralized version control — every commit lives on a central server. Git is distributed version control — everybody has a copy of the repository. When you commit to subversion, the push to the central server is implicit. The commit and push are discrete actions in Git. This means you can do work and commit changes without necessarily being connected to the Git server. Another advantage is the ability to make commits locally without necessarily publishing your work.

File vs Change-based version control

Subversion is file-based — when you make changes and run svn commit, it commits all the changed files by default. Git is changed-based — when you make changes, you have to run git add to “stage” changes. You can get similar behavior to Subversion with git commit -a. The “a” flag tells Git to add all changes in tracked files.

Commands

  • init – Create a new repository.
  • clone – Set up a repository that already exists on a remote server.
  • add – Stage changes to be committed.
  • commit – Add changes to the local repository.
  • push – Push unsynced commits to a remote repository.
  • pull – Pull unsycned commits from a remote repository.

Basic Workflow

  1. Get the latest version of the code. If you already have the repository, you can use git pull to get the latest changes. Otherwise, git clone will download the code from a remote repository and set it up locally. If this is a new repository, you can set it up with git init.

  2. Make changes.

  3. Share your changes. git add <file>; git commit; git push or git commit -a; git push.

Hour of Code

This week 15 million students learned to code.

A friend of mine called me Wednesday night to ask if I was available to talk to some of his high school students about Computer Science and hangout while they worked on the Hour of Code. They have no Computer Science curriculum so this could be one of a few times they could experience Computer Science first hand in high school.

I didn’t really know what Computer Science was when I started studying it, so I think getting kids exposed to it while they’re still deciding what they want to study can only be a good thing.

We started by showing a promotional video for Hour of Code.

Then they started programming.

One group got in way over their heads. I noticed right away that they were writing Objective-C, so I asked if they had written any iOS apps, or even had any programming experience. They didn’t. My first thought was that starting with Objective-C without any experience would be pretty hard, but I offered to answer any questions they had. The next time I walked past, they flagged me down. They needed to instantiate an object.

Again, I thought it would be pretty hard to write Objective-C without understanding what Objects are — or, even the basic things you learn in the first week of any programming class. I gave a basic description of objects and helped them move on to the next step.

The next time I walked past, they had a ship flying on the screen and were working on creating a guy to shoot at the ships. They asked me a few more questions throughout the hour or so that I was there and by the end they had gotten pretty far through the iOS tutorial with ships flying back and forth and a guy to shoot at them when you tapped the screen.

It was a fun day answering questions about Computer Science and hopefully convincing a few of them to dig into it some more on their own.

Minneapolis

WordCamp Milwaukee 2013 Slides

On June 8, 2013 I gave a talk about automating your WordPress development Workflow at WordCamp Milwaukee. Here are the slides along with some notes.

As I’ve mentioned before, computers are really good at carrying out repetitive tasks and we should use that to our advantage to make development easier.

We all know you should develop locally and on the latest development version of WordPress. But in order to checkout the trunk, you have to use SVN — unless you know about git-svn. You can checkout the entire core repository with:

git svn clone -s http://core.svn.wordpress.org

That’s going to take a while because it is going to download all the WordPress history from forever, but once it’s done it’s just another simple git-svn command to keep it up to date:

git svn rebase

You can also put git repositories inside other git repositories. This is useful so you can use a different repository for each plugin and theme. Just initialize a new repository in the themes or plugins directory and it will work just like normal — even if you’re already using git to manage your development version of WordPress.

Automate your Environment

If you like using a virtual machine for your local development environment, vagrant has to be the way to go. A simple configuration file defines the virtual machine and vagrant, which relies on VirtualBox, takes care of setting it up. Pair that with provisioning software like Puppet or Chef and you’ve got a development environment that you can get up and running with a single command.

Vagrant depends on a Vagrantfile that defines the virtual machine. It’s important to note that the folder that the Vagrantfile lives in will be shared using VirtualBox’s built in sharing on the guest to /vagrant.

Puppet scripts define how a server should look. So, “has apache, php, and mysql installed” is an example. The idempotence of puppet is really cool. You can run a puppet script over and over again and it won’t break anything. If you say a server should have nginx and it already has nginx, puppet will just skip that step. The weird part about puppet, and the thing I found most confusing at first, was that stuff doesn’t run in order. You have to clearly specify dependences because the software is going to optimize your script as much as possible. This gets easier to follow once you work with it for a while.

My current setup is at https://github.com/joshbetz/WCMKE–2013-Vagrant-Puppet

When everything is configured, simply run vagrant up and vagrant will start the virtual machine. Other commands are vagrant ssh, vagrant halt, vagrant destroy, and vagrant provision. SSH is straight forward. Halt shuts down the machine. Destroy removes the machine from VirtualBox. Provision runs the provisioner, whether that’s Puppet, Chef, or a shell script.

This is really powerful. Like your dotfiles, you can put this in the cloud and download the configuration when you need it. And simply run vagrant up to start the machine.

The other nice thing is that our development environments can be open source now. Working together on this stuff is a powerful thing, but I don’t think I have to convince the WordPress community of the power of open source.

Automate your Development

Since we are talking about WordPress development, I’m also going to mention some ways you can make the software work for you while you’re writing code.

  1. Developer Plugin – http://wordpress.org/extend/plugins/developer/
    It’s maintained by Automattic and basically just helps you set up your local development environment the right way. From checking your wp-config.php for the proper constants, to verifying that you’re on the latest development release of WordPress, to recommending awesome plugins that make WordPress development run more smoothly.

  2. Theme Unit Test – http://codex.wordpress.org/Theme_Unit_Test
    This one is maintained by the WordPress Theme Review Team. Even though it’s called the Theme Unit Test, this is useful for all developers. It’s a collection of a huge range of content for you to import into WordPress. Even if you’re developing the next hit plugin, you’re going to need content to test on. There are posts of every post format and any kind of example you could think of.

  3. Underscores.me – http://underscores.me
    Something else that is maintained by Automattic. While it’s technically a theme, I think of it more as a collection of awesome snippets that you can drop into your theme. That’s not to say you couldn’t download this and style it the way you want and be done. It would probably be great as a theme by itself, but there’s so much awesome in here that I just like to scan through the code once in a while and see what new magic I find.

So, to wrap this up, make your computer work for you. Automate everything that can be automated unless you know you’ll never need to do it again. When you do something twice, that’s a good sign that you’ll probably do it again. 🙂