1. Technology
Send to a Friend via Email
You can opt-out at any time. Please refer to our privacy policy for contact information.

Using Git With Ruby on Rails Part 2: Branches

By

Using Git With Ruby on Rails Part 2: Branches

This is the second part in a short series on using Git with Ruby on Rails. Before diving in, check out the first part.

A branch is a separate set of changes from the "master" branch. I like to think of branches as experiments. To start a new experiment, you start a new branch. You can make multiple commits to this branch, as many as you'd like. Then, if the experiment was successful, you can merge that branch to the master branch. Or, if the experiment was a failure (they often are, and this is the beauty of using something like Git, it's dead simple to undo the damage) you can check out the master branch and delete the experimental branch.

Successful Experiment

So let's create a new branch called an_experiment. This is done by calling git checkout -b an_experiment, this will both create the new branch and check it out, making it the active branch. We'll then edit some files and I'll show you how to delete the branch or merge it back into the master branch.


$ git checkout -b an_experiment
switched to a new branch 'an_experiment'
$ # Make some changes to a few files
$ git status
# On branch an_experiment
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   config/boot.rb
#	modified:   config/database.yml
#	modified:   config/environment.rb
#
no changes added to commit (use "git add" and/or "git commit -a")
$ # The experiment was a success, let's merge the experiment into master
$ git commit -a -m "Experiment was a success!"
$ git checkout master
$ git merge an_experiment
Updating c1f00e1..1997a2b
Fast-forward
 config/boot.rb        |    1 +
 config/database.yml   |    1 +
 config/environment.rb |    1 +
 3 files changed, 3 insertions(+), 0 deletions(-)

OK, that's easy enough. A good workflow when working on Ruby on Rails projects is to create a branch, commit changes (preferably multiple commits, if it's just one commit you really don't need a branch), switch back to master and merge the experimental branch. But there's one more thing, the experimental branch is still there. Run git branch to list the branches. It's up to you whether you want to keep the branch. If you want to delete it, run git branch -d an_experiment.

Failed Experiment

So, let's pretend the experiment was a failure. Say you generated a new controller but you've decided your approach is all wrong and want to erase the evidence of your failure. The process here is almost the same, we'll switch back to the master branch and we'll either ignore the failed branch (you can refer to it later, it doesn't hurt anything to keep around) or delete it. Note that there are two -d switches. The -d switch will only delete a branch if it's been merged, while the -D switch will always delete the branch, regardless of the merge status. Since we specifically do not want to merge our failed experiment, we'll use the -D switch.


$ git checkout -b another_experiment
$ # Edit some files
$ git commit -a -m 'Well, that was a disaster'
$ git checkout master
$ git branch -D another_experiment

The only thing to remember here is that, even though the experiment was a failure, to commit the changes to the experimental branch. If you switch back to the master branch without committing, you have changed files and have to check them out again. If you do do that, however, you can quickly revert all changes to all files to their state in the master branch (or whatever branch you're currently on) using git checkout .. This is similar to git add ., by checking out the current directory, it will recursively check out all files. This is a good shortcut to remember, git checkout . will bomb all modified files, erasing all changes and reverting the files to their committed versions.

  1. About.com
  2. Technology
  3. Ruby
  4. Ruby on Rails
  5. Ruby on Rails 3 Tutorials
  6. Using Git With Ruby on Rails Part 2: Branches

©2014 About.com. All rights reserved.