I've been slowly trying out git and Github and doing some experimenting. Tonight I tested the 'branch' waters. There seems to be some confusion on some blogs regarding managing branches on Github. I think I've got it figured out (at least for my purposes), so for future reference and to maybe help out someone else, here are my notes. This post and this one have some tips that helped me a lot, but they're still a bit confusing and I'm not convinced the authors were very sure what they were doing when they wrote those. (caveat: I'm not really sure what I'm doing either.)
My goal was simply to be able to easily create a local branch, easily commit changes on the branch to Github, and easily merge the branch back into the master. I'll assume that if you want to follow along, you've already got a repository setup on github, a local copy of that repository on your machine, and that you're at the command line in the projects directory, and you're on the master branch. Ok, let's do it.
#check that we're on the master branch > git branch * master #create a local branch named flibberty > git branch flibberty #confirm new branch exists > git branch flibberty * master #push the new branch to github > git push origin flibberty
At this point, if you go to your repository on Github and click 'all branches' you should see 'flibberty' in the list. (If you already had the page loaded, you'll need to reload the page first. Ok, so now lets do something with our branch.
#switch to flibberty branch > git checkout flibberty # Now make some changes to a file and then... > git commit -a -m "test committing changes to a branch" > git push #switch back to the master > git checkout master # Make a different change to a file here, and then > git commit -a -m "test change committing back to master" > git push
Ok, now at this point you should be able to swap back and forth between your master and flibberty branches on Github and see the different changes in master and flibberty. You can also go the the "Network" tab and see the map that shows the branch commits parallel to the master. But, wait, what if your co-worker created this branch, and all you've got locally is the master? How can you switch to the branch to make your own changes?
# create a local branch tied to the one on Github and switch to it. > git branch flibberty origin/flibberty > git checkout flibberty
Now, let's imagine we're done making changes to the branch and ready to merge it back into the master.
> git checkout master > git merge flibberty > git push
And that's it. The key point here is getting the local branch onto Github, which despite convoluted instructions elsewhere, seems to just be one simple step... git push origin local_branch_name. Easy, huh? One last thing. We're done with the branch, so now we can safely delete it from Github and locally.
# delete branch from Github (notice the colon!) > git push origin :flibberty # delete local branch > git branch -d flibberty
I think that command to delete the branch on Github is about is cryptic as it can get, which is my one complaint with git so far. After a couple years of working with Ruby and Rails and getting used to easy to read a lines of code like @user.subscriptions.find(:all, :conditions => {:active => true}) git has been a rude reminder that some programmers seem to go out of their way to be as opaque as possible... and the people that created git (Linus, I'm looking at you!) are apparently in that club.

