More Version Control with Git

Branches

Learning Objectives

  • Understand branches.

We use the name branch to keep the same notation of set theory since the Git history is a tree. Unfortunately the term branch could be better understood if we think of thumbtack that has a name.

Picture of thumbtack and a piece of paper with the text The task ahead of you is never as great as the Power within you. The picture is called An inspirational message over the sink by Bunny Jager available at https://flic.kr/p/88f39H under CC-BY-SA.

Picture of thumbtack and a piece of paper with the text “The task ahead of you is never as great as the Power within you.” The picture is called “An inspirational message over the sink” by Bunny Jager available at https://flic.kr/p/88f39H under CC-BY-SA.

We can add and remove thumbtack on a tree as we like. We can do this with Git and we will learn how soon.

Thumbtack on a branch tree. The picture is called Thumbtack by Christian Bucad available at https://flic.kr/p/7XiB1R under CC-BY-NC-ND.

Thumbtack on a branch tree. The picture is called “Thumbtack” by Christian Bucad available at https://flic.kr/p/7XiB1R under CC-BY-NC-ND.

If our thumbtack is on a leaf of the tree and the tree grows our thumbtack will change its position. Git has this same behaviour.

We can get the name of the thumbtacks on our Git repository with

$ git branch --all
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

The star, *, marks the thumbtacks that has the files that we are using now.

If we want to get where the thumbtacks are on our Git tree we can use

$ git lga
* 3f3c1bf (HEAD -> master, origin/master, origin/HEAD) Add words
* 22dcdb5 Add words
*   92808ea Add words
|\  
| * 5121eef Add words
* |   ee40506 Add words
|\ \  
| |/  
|/|   
| * 51bd45c Add words
| * ee87332 Add words
* | 87db327 Add words
|/  
* cde9009 Add Python \o/
* 23c6a6b Add words
* 6318870 Begin

So, the thumbtacks master, HEAD, remotes/origin/HEAD and remotes/origin/master are on the commit 3f3c1bf.

The file en.dic doesn’t have the word abduction. Lets create a branch, called abduction, where we will add that word.

To create the branch we use

$ git branch abduction

If we list our branches,

$ git branch --all
  abduction
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

we will notice that we still at the master branch. To change our current branch we use

$ git checkout abduction
Switched to branch 'abduction'

After we add the word abduction to en.dic we can create a commit with that change.

$ git commit -am "Add abduction"
[abduction fb8dde2] Add abduction
 1 file changed, 1 insertion(+)

We can push our branch to GitHub:

$ git push origin abduction
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:rgaiacs/YYYY-MM-DD-git-sample.git
 * [new branch]      abduction -> abduction

And we can check our Git tree:

$ git lga | head -n 3
* fb8dde2 (HEAD -> abduction, origin/abduction) Add abduction
* 3f3c1bf (origin/master, master) Add words
* 22dcdb5 Add words

Notice that the thumbtack HEAD and abduction change their position and a new thumbtack, origin/abduction, is on our tree.

Detached HEAD State

Consider the following commands:

$ git checkout 3f3c1bf
Note: checking out '3f3c1bf'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 3f3c1bf... Add words
$ echo camp >> en.dic
$ git commit -am 'Add camp'
[detached HEAD 8413095] Add camp
 1 file changed, 1 insertion(+)

Explain what happened. After that send the new commit to GitHub in a branch called camp.

More branches

Create branches called genomic and voluntary that should start at the commit 3f3c1bf. At each branch, add one commit that add the name of the branch into en.dic. After create the two branches, push them to GitHub.