(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"
B. Integrate with the latest
- 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.
C. Merge with the rest of the team
- git checkout master
- git merge dev-branch-name
- git push
It looks like a lot of steps and a lot to remember, but you can automate much of it if you'd like to save some keystrokes. I still like to go through the motions. You could use a simpler, more traditional (read: SVN) workflow, doing all of your work in master, but then you're missing out on a lot of the benefit of Git.
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.