Version Control with Git

Branching

Learning Objectives

  • Create new branches.
  • Switch between branches.
  • Merge branches.

git branch : Listing, Creating, and Deleting Branches

Branches are pointers to a version of a repository that can be edited and version controlled in parallel. They are useful for pursuing various implementations experimentally or maintaining a stable core while developing separate sections of a code base.

Without an argument, the branch command lists the branches that exist in your repository.

$ git branch
* master

The master branch is created when the repository is initialized. With an argument, the branch command creates a new branch with the given name.

$ git branch experimental
$ git branch
experimental
* master

To delete a branch, use the -d flag.

$ git branch -d experimental
$ git branch
* master

git checkout : Switching Between Branches, Abandoning Local Changes

The git checkout command allows context switching between branches as well as abandoning local changes.

To switch between branches, try

$ git branch newbranch 
$ git checkout newbranch 
$ git branch

How can you tell we’ve switched between branches? When we used the branch command before there was an asterisk next to the master branch. That’s because the asterisk indicates which branch you’re currently in.

Also, there’s a neat trick using .bashrc which is way better. To never wonder again, put this in your ~/.bashrc file:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/^* \(.*\)/(branch: \1)/'
}

PS1="\$(parse_git_branch)\n$ "

git merge : Merging Branches

At some point, the experimental branch may be ready to become part of the core or two testing branches may be ready to be combined for further integration testing. The method for combining the changes in two parallel branches is the merge command.

Exercise : Create and Merge Branches

Step 1 : Create two new branches and list them

$ git branch first
$ git branch second

Step 2 : Make changes in each new branch and commit them.

$ git checkout first
Switched to branch 'first'
$ touch firstnewfile
$ git add firstnewfile
$ git commit -am "Added firstnewfile to the first branch."
[first 68eba44] Added firstnewfile to first branch.
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 firstnewfile
$ git checkout second
Switched to branch 'second'
$ touch secondnewfile
$ git add secondnewfile
$ git commit -am "Added secondnewfile to the second branch."
[second 45dd34c] Added secondnewfile to the second branch.
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 secondnewfile

Step 3 : Merge the two branches into the core

$ git checkout first
Switched to branch 'first'
$ git merge second
Merge made by recursive.
 0 files changed, 0 insertions(+), 0 deletions(-)
  create mode 100644 secondnewfile
$ git checkout master
Switched to branch 'master'
$ git merge first
Updating 1863aef..ce7e4b5
Fast-forward
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 firstnewfile
 create mode 100644 secondnewfile