Friday 20 July 2012

Git Branches

Branching is definitely one of the powerhouse aspects of Git. This will be a brief post on how to create, checkout and commit to a new branch. And then merge the changes back into your master branch.... Should you need to.

Example. Spiking a potential technical solution to a future, unimplemented feature. (indicated by 'feature_x' below)
Visualizing branching (image from gitguru.com)

$ git checkout -b feature_x
Switched to a new branch "feature_x"

What's happened here is you've told Git to create a new representation of your project off the main development or working branch. Everyone else can continue to work on that branch, commit things to it, push those commits, etc... You can also keep working on that main branch and pulling down updates on it as well.

View all your branches (The star indicates the branch you're currently on):

$ git branch -a
* feature_x
  master

If you're main development branch is called 'master' this is how you move back and forth between branches your two branches.

$ git checkout master 
Switched to branch 'master'

$ git checkout feature_x
Switched to branch 'feature_x'

Now, at the moment your branch is stored only locally on your machine. If you want someone else to be able to view it or commit to it, you need to push it to your remote repository or 'origin'.

$ git push origin feature_x

Ok, let's talk about how you work on this new branch and move things between it and master.

First, check the new branch back out

$ git checkout feature_x

Switched to branch 'feature_x'

Now, write some code.... la la la... write some more code.....

You can see what you've done so far with

$ git status

# On branch feature_x
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# new_file.rb


Now, add the files to the staging area and commit the code into the git directory for this branch.

$ git add .
$ git commit -m"JY - my code changes"
[feature_x 2c4674c] JY - my code changes
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new_file.rb


In my earlier post, I talked about how the git DB keeps track of your git directory. When you have multiple branches you can imagine that that DB is tracking each different branch as a different git directory. One way of viewing these directories is using 'git log'.

$ git log
commit 2c4674cf17fd0b7a950076023c810e9796400e0a
Author: John Doe <john.doe@gmail.com>
Date:   Fri Sep 28 12:14:57 2011 -0400

    JY - my code changes
.
.
.

You can checkout master again and run the same command there to confirm that it is, in fact, only on your feature_x branch. But one thing to notice when looking at these commits listed under the git log is that each one has unique identifier associated with it. I'll wrap this post up by showing how you can pull commits from one branch to another by using the cherry-pick command. 

$ git checkout master
Switched to branch 'feature_x'
$ git cherry-pick 2c4674cf17fd0b7a950076023c810e9796400e0a
[master efc5626] JY - my code changes
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new_file.rb 

You can see that that commit has been added to master by doing another 'git log'. Notice that the commit has a different unique identifier. So, it's unique to it's branch within the git directory/db.

$ git log
commit efc562669b7dd993c0a4088144d6f008fde66c0e
Author: Julie Yaunches <julie.yaunches@thoughtworks.com>
Date:   Fri Sep 28 12:14:57 2012 -0400

    JY - my code changes
.
.
.

No comments:

Post a Comment