Saturday, December 11, 2010

When and how to create and merge branches on git

The Scenario

Here's the scenario: I want to add a new and different authentication system (Devise) to wikidiscography.com. Should I just commit any outstanding changes I've made, then do the entire switchover, then do another commit, so that all my Devise additions are in one neat little commit? No, probably not. Why? Because I'm not a great coder or a great user of Rails, so my attempt to install and configure Devise will probably take several days. Sometime in the middle of the switchover I might find that I have to make other changes, so those changes would be rolled into the Devise git commit. My understanding of git best practices is that changes should be committed as logical groups. So what's the answer? I think the answer is to create a new branch, make the Devise changes there until it works properly, then merge that new branch back into the master branch. Simple enough.

NB: For a great explanation git and how to use it, see http://gitref.org/

Make a new git branch

Much of this is learned from http://gitref.org/branching/
From the command, from within the application folder, create a new git branch and call it "devise":

$ git checkout -b devise


(The -b flag creates the new branch and immediately switches over to it). Check that you're in the correct branch with $ git branch, and see that the asterisk is next to the "devise" branch. Now we can safely delete files and destroy the application, and if we make irrecoverable mistakes, we can just delete the branch and clone the original repository from Git.

But now that I've been working on the Devise configuration for a while, I find that I have to make another major change that again really deserves its own separate commit or even its own branch. I have to rename two sets of models/controllers/views from "session" and "session_type" to "msession" and "msession_type," respectively. But I'm already in the Devise branch; can I just make another branch, make the renaming changes, and then merge back? Do I merge it back to the Devise branch, or back into the master branch? Let's just move forward and see what happens.


Make another new git branch

From the terminal again, type
$ git checkout -b rename_session_to_msession

and ensure you're in the new branch with git branch. Now go through all the code and replace "session" with "music_session" everywhere. I also did this with migrations, which probably wasn't the best way to do it. I think the Rails way would have been to create another migration in which I renamed the tables and the table columns. This might cause problems later when I try to push everything to Heroku. But for now, it's enough for me to get my development environment working.

After going through the application and making sure everything still worked now that "session" has been replaced with "music_session", I commit my changes.
$ git commit -am "changed 'session' to 'music_session' everywhere".

Now I have a branch with a committed set of changes.


Merge the branch

Now I want to merge this branch back into the "devise" branch. First switch to the "devise" branch:
$ git checkout devise

Then tell Git to merge the "rename_session_to_msession" branch into the "devise" branch:

$ git merge rename_session_to_msession

Now I'm done with the "rename_session_to_msession" branch, so I can delete it:

$ git branch -d rename_session_to_msession

Now my "devise" branch shows the renamed session items, so I can continue working on the Devise configuration.

Done.

No comments:

Post a Comment