(Or, how I learned to stop worrying and love the Git.)
Git is awesome. If you're using it, you know what I mean. If you're not, don't be afraid to give it a try! Git is the gateway to a new generation of code sharing on GitHub and convenient deployment to Heroku. It's built around fast branching, so it's really easy (and did I mention fast?) to create and throw away experimental branches.
I'm pretty new to Git. I used it a bit in the past (with GitHub) but hadn't really gotten into it until the past month or so when we made the switch at PatientsLikeMe from Subversion. It took me a couple of weeks to really understand what was going on, feel comfortable, and settle into a rhythm and workflow. This post will probably be old news to Git veterans, but I think there are enough Git noobs still out there that some will find this interesting or valuable (let me know if you're one of them!).
Here's how I use Git day-to-day, whether for new feature development or fixing bugs:
A. Work in a local branch
- git checkout -b dev-branch-name (to create and switch to a local branch, where "dev-branch-name" is something meaningful)
- Do some awesome (more or less) work.
- git status, git add, git commit -am "Something about what I did"
- git checkout master
- git pull (gets the stuff my teammates have pushed)
- git checkout dev-branch-name
- git rebase master (integrates my work with my teammates')
- Run the test suite.
- git checkout master
- git merge dev-branch-name
- git push
To make git a bit more user-friendly, I set up most of the aliases suggested by Justin French, so I actually use "git co" for checkout and "git ci" for commit, and I have a git completion bash script set up so that I don't have to type out full branch names.
Switching between master and various local branches can get a bit confusing. To keep myself on track, and to try to avoid working in the wrong branch, I set up a shell prompt that displays the current branch for the git-aware working directory that I'm in. Here's the actual prompt I'm using:
function parse_git_branch
{
ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
echo ${ref#refs/heads/}
}
export PS1='\[\033[0;34m\]\A \[\033[00m\]\w \[\033[01;31m\]$(parse_git_branch)\[\033[00m\] $ '
I've also started using grb to make working with (read: tracking) remote branches easier. It's easy to install and has good documentation to get you started. Then there's GitX, a powerful tool to browse your repository and to see which code is on what branches. I don't lean on GitX as much as I could, but it comes in handy.
There's a lot about Git that I still need to learn, so please leave a comment with your tips and tricks.

4 comments:
Great post Jeremy. Appreciate the tips for working smarter. Thanks for the heads up on GitX too.
My biggest problem with git is still the graphicsl tools support. If you're not in macosx you're pretty much toast.
TortoiseGit is decent on windows, but on linux the best tool I could find was KGit, which is horrible. No decent eclipse plugin either, which is a deal-breaker for me.
I'll stick with mercurial solely for mercurialeclipse which isn't amazing but beggers can't be chosers.
Dean, thanks, glad you enjoyed it!
Anonymous, as I mentioned in the post I rarely even use GitX. I typically stick with the command line (which any serious Linux or Mac user should be comfortable with). I occasionally use a TextMate bundle for simple tasks, especially for the "blame" feature.
I haven't used Git on Windows at all, so I can't speak to it, but doesn't surprise me at all that Windows support is lagging. I've heard some good things about Hg, which has some of the same distributed VCS benefits as Git.
I dont know why, but branching has never clicked with me until reading this post. Thanks!
Post a Comment