tags:

views:

113

answers:

1

Coming from an svn background: I hardly ever branched, due to the (lack of) speed of switching and the hour or more it took to merge branches back into the trunk. Sometimes, if I needed to hotfix a problem on a web site, I'd make the change in the trunk (which would live along with previous changes or new features) and then go to that file and just do "svn up path/to/filename" and it would update only that file, fixing the problem but sparing the rest of the files.

Conceptually, this doesn't seem possible in git (or necessary); is it structured staging and grouped commits that allow for cherry-picking? So, I might change a specific area of the site and commit it as a group instead of doing how I work with svn, and go about a day's work and touch files all over the commit the whole batch at once?

+2  A: 

What you probably want to do in this case is make a new branch for your hotfix, branching off of the common ancestor of all branches which will need the fix. For example, suppose you have a couple maintained releases along with your current development:

- X - o - o - o - o - o - o - o - o - o (master)
   \                       \
    o - o - o (release A)   o - o (release B)

If you need to make a hotfix which will apply to both releases, create a branch starting from the commit labeled X. Commit your fix, then merge that branch into all three branches.

You could cherry-pick, but here's a good rule of thumb about when to cherry-pick: don't. The only case in which you will want to cherry-pick is when you've managed your branches badly. In this case, that might mean that you made the fix on master instead of properly branching it off from an earlier point, and people have already pulled the updates to master, so you can't change it. You'd have to cherry-pick to get it on the two release branches. But of course, you should just manage your branches right in the first place, and you'll never need to cherry-pick. (Yes, it'll still happen sometimes; such is life.)

Jefromi
@Jefromi, that's awesome. I just played around with some "git branch foo old-merge-sha1" stuff and that's just amazing.
Hans
Is it best practice to create a branch off master for "FeatureA" then branch off "FeatureA" again for topic(s), merging them into FeatureA when they are ready, but not touching master until the entire FeatureA is ready, when you'd merge FeatureA into master? Right now (in svn) if I'm working on something that is going to take a few weeks and need a hotfix, I do what I described in my question (svn up path/to/filename) and then move on. Git branching seems to be a "merge often" recommendation, but what if I don't *want* the master/trunk touched until ready, so I can freely update the web site.
Hans
@Hans: You don't merge until, well, it's time to merge. Usually master is a current stable branch, so you could move a partial but stable feature if you wanted, but in general you should only merge complete things. And it's quite possible to have several stable branches of different flavors - one for a given relase (for your web site), one for current stable testing, one for unstable testing... just make sure always to merge in the right direction (topic -> feature -> master or unstable -> stable).
Jefromi
@Hans: You might find Junio Hamano's article on branch/merge philosophy helpful: http://gitster.livejournal.com/42247.html and you might also like this article about a "branching model" http://nvie.com/git-model
Jefromi
@Jefromi Excellent input, thank you.
Hans