My new love affair with Git

Recently, within the last six months or so, I discovered and started using Git. Git is a distributed version control system that allows you to keep a local version of your project and push and merge changes to a centralized repository. There are other workflows, but so far I've only used a centralized version model since I'm typically working by myself on the projects I do. There are dozens of reasons to love Git, and I thought I'd share a few.

1. Local copies of your repository
With a local copy of your repository, development goes much faster. I can work locally and push to the remote repo when I need to without having to wait for a network connection to commit my changes.

2. Design your own workflow
Because I work by myself on the majority of my projects, I can start a project locally, create a SQL dump of the database and then create a git repo of all of the necessary files. After I've created my local repo I usually use Codaset or Github to store a remote repo of the project as well.

3. Easier and faster deployment
Once you push your files to the remote repository, it's easy to get your files onto the server. Just ssh in, and clone a copy of the repo right where you want it. You can then use the sql dump you created to set up the site identically to your local setup. Once you've set up the site, it's easy to make all changes locally and pushed through your process. This works best on sites in development because changes to the database are difficult if not impossible to version once the site goes live. There are other strategies that deal with this issue, but those are beyond the scope of this article.

In a one man shop you probably won't take advantage of all of Git's features, but as you write more (especially experimental) code, it's useful to be able to fork and deploy code without a lot of overhead.

Here are some useful Git commands that I use on a regular basis.

git init
git status
git add .
git commit -a
touch tmp/.gitignore log/.gitignore vendor/.gitignore
git remote add origin (or codaset, github etc)
git@username:codaset.com/nameofproject.git
git push origin master
git remote show

You can even alias these and other commands in your .profile (Mac OsX) or .bashrc (Linux) to make them even faster and easier to use.

alias gst='git status'
alias gl='git pull'
alias gp='git push'
alias gd='git diff | mate'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gb='git branch --color'
alias gba='git branch -a'
alias gco='git checkout'