Supplemental: Using Branches with Git

Overview

Teaching: 10 min
Exercises: 15 min
Questions
  • What is branches and how to use it ?

Objectives
  • Understand how to use branches with Git

In very simple terms, git branches are individual projects within a git repository. Different branches within a repository can have completely different files and folders, or it could have everything the same except for some lines of code in a file.

Let’s use a few real world examples (at least that I’ve used before, others may have used them differently):

How it works

The nice (and very powerful) thing about Git is the fact that branches are very cheap compared to other version control systems. By cheap, I mean they don’t take up much disk space, it’s computationally easy to move between branches, and it’s (relatively) easy to merge branches together. This is because of how Git represents branches, since they are simply pointers or an individual commit. That’s it. Just a pointer. Git commit history is a directed acyclic graph, which means that every single commit always has a ‘parent’ commit (the previous commit in the history, or multiple parents when a merge happens), and any individual commit can have multiple ‘children’. This history can be traced back through the ‘lineage’ or ‘ancestry’. The branch just gives a name to each ‘lineage’ when a commit has multiple children.

When you merge two branches together, the commit histories get merged together as well. Which means that all the changes you made in each branch gets combined back into a single lineage, rather than two. This makes it easier to work collaboratively on a project, since each individual could work on their own branches, without dealing with the messiness that could come from working all on one branch.

Let’s start : Create a new branch

Our mars.txt file

$ cat mars.txt
Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman
But the Mummy will appreciate the lack of humidity
An ill-considered change

Create a new branch named rocket

$ git branch rocket

So far nothing really appends :

$ git status
On branch master
nothing to commit, working tree clean

We need to change the branch

$ git checkout rocket
Switched to branch 'rocket'

We can edit the mars.txt file and add something about the rocket project

$ nano mars.txt
$ cat mars.txt
Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman
But the Mummy will appreciate the lack of humidity
An ill-considered change


The project of rocket will need a spaceship and a motor
Father might have the budget for it
We need to also to move material and animals

Then we add and commit changes

$ git status
On branch rocket
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   mars.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git add mars.txt
$ git commit -m "start the rocket project"
[rocket 468dd5c] start the rocket project
 1 file changed, 6 insertions(+)

This modification only affected the rocket branch if we go back to the master branch the mars.txt file is unchanged.

$ git checkout master
$ cat mars.txt
Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman
But the Mummy will appreciate the lack of humidity
An ill-considered change

Branches are useful tools for working on different version of a project and to switch back and forth to it.

Merging branch

Merging branches means to include the change of one branch to another.

We go to the destination branch (masterhere)

$ git checkout master
Switched to branch 'master'

And we merge rocketbranch into it

$ git merge rocket
Updating dbd9912..468dd5c
Fast-forward
 mars.txt | 6 ++++++
 1 file changed, 6 insertions(+)

mars.txt changed

$ cat mars.txt
Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman
But the Mummy will appreciate the lack of humidity
An ill-considered change


The project of rocket will need a spaceship and a motor
Father might have the budget for it
We need to also to move material and animals

you can delete the rockerbranch

$ git branch -D rocket
Deleted branch rocket (was 468dd5c).

Key Points

  • git branch create branches

  • git checkout change the branch

  • git merge merge branch

  • git show-branch display branches