The Free, Online Git / GitHub 101 Course

( Link to Day 7 - http://goo.gl/dIYhk )

DAY 8


Branching
The true power of Git comes out when you start using branches. The ability to easily and quickly create and manipulate branches is one of Git's major selling points.

Branching means you diverge from the main line of development and continue to do work without messing with that main line.

Open a Bash shell in the folder gitlocalrepo/gittest and type:
$ git branch

It says that you are working in the master branch. The default branch name in Git is master.


Creating a Branch
Let us now create a new branch called student where we would work on a new feature in a file student.rb

To create the student branch, type in your open Bash shell window:
$ git branch student

To confirm, type:
$ git branch
* master
student

Notice, that the branch student has been created but we are in the master branch still.


Switch to a Branch
Let us now switch to the student branch by typing:
$ git checkout student

To confirm, type:
$ git branch
master
* student

Let's create a simple student.rb file in the folder gitlocalrepo/gittest as follows:
# student.rb
class Student

end

Now type:
$ git add student.rb
$ git status
# On branch student
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: student.rb
#
$ git commit -m "student.rb first commit"

Let's now push this to our repo on GitHub. The syntax is:
$ git push origin <localbranch>:<remotebranch>

And so this operation is really "pushing to the remote student branch."

Type:
$ git push origin student

See the snapshot of my repo on GitHub. Notice that the student branch has been added:
http://www.twitsnaps.com/1075941

In our local repo, let us switch back to the master branch and check what the difference is between the master and student branches.
$ git checkout master
Switched to branch "master"
$ git diff master student
diff --git a/student.rb b/student.rb
new file mode 100644
index 0000000..84578c6
--- /dev/null
+++ b/student.rb
@@-0,0+1,4 @@
+# student.rb
+class Student
+
+end

As you can see it says that a new file student.rb exists in the student branch.


Merging a branch
Let us assume that you have finished working in your student branch and would like to merge the changes made in student into master.

To achieve this, type:
$ git checkout master
Already on "master"
$ git merge student
Updating df96832..6295c5d
Fast-forward
student.rb | 4 ++++
1 files changed, 4 insertions(+), 0 deletions(-)
create mode 100644 student.rb

If the changes don't conflict, you're done. If there are conflicts, markers will be left in the problematic files showing the conflict. Once you've edited the files to resolve the conflicts, type:
$ git add .
$ git commit -m "Commit after student branch merge"

will commit the result of the merge.


Deleting a branch
At this point you could delete the student branch with:
$ git branch -d student
Deleted branch student

Now push the changes to the master repo on GitHub by typing:
$ git push origin master

Now we can safely remove the student branch from our repo on GitHub by typing:
$ git push origin :student
To git@github.com:SatishTalim/gittest.git
- [deleted] student

We confirm this by checking out the screenshot:
http://www.twitsnaps.com/1075944


Exercise
In the master branch of your local repo gittest, start writing a Ruby program called motorcycle.rb as shown:
https://gist.github.com/1234158#file_motorcycle.rb

Stage this file, commit it and push it to your gittest repo on GitHub.

Create a local display branch and in this branch, write display.rb as follows:
https://gist.github.com/1234158#file_display.rb

Add this file to the stage. Next commit it to your local repo.

Push the display.rb file in branch display to the gittest repo and branch display on GitHub.

Now we have finished working in our display branch. Merge the changes made in display branch into master branch and delete the display branch for your local repo. Add all files for staging and commit to your local repo.

Finally, push the changes to the master repo on GitHub and delete the display branch on GitHub.

Good Reference: http://learn.github.com/p/branching.html


Remember:
1. The link to Day 1 of the course is http://goo.gl/k8wwz
2. The link to Day 2 of the course is http://goo.gl/5z5AX
3. The link to Day 3 of the course is http://goo.gl/jDibu
4. The link to Day 4 of the course is http://goo.gl/ewqEj
5. The link to Day 5 of the course is http://goo.gl/2jbA8
6. The link to Day 6 of the course is http://goo.gl/CnBKW
7. The link to Day 7 of the course is http://goo.gl/dIYhk
8. Please +1 the button below, so that I know you have completed Day 8 and that I can post the Day 9 material here.
9. Please post your suggestions / feedback / problem areas as comments to this post here.
Shared publiclyView activity