Git/C2/Branching-in-Git/English

From Script | Spoken-Tutorial
Revision as of 10:53, 31 July 2015 by Priyacst (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Branching in Git

Author: Priya K

Keywords: Video tutorial, branching, master branch, creating branch, * master, switch branches, --force


Visual cue
Narration
Slide 1:

Branching in Git

Welcome to the spoken tutorial on Branching in Git.
Slide 2:Learning Objectives


In this tutorial, we will learn about


  • Branching
  • Creating a branch and
  • Switching between branches


Slide 3:

System requirement


For this tutorial, I am using
  • Ubuntu Linux 14.04
  • Git 2.3.2 and
  • gedit Text Editor

You can use any editor of your choice.

Slide 4:

Pre-requisites


To follow this tutorial
  • You must have knowledge of running Linux commands on Terminal.
  • If not, for relevant Linux tutorials, please visit our website.


Let us learn about branching.
Slide 5a:

Branching


* Typically, branches are used in a project to develop new modules or to fix a bug.
  • It helps to work with new modules of a project without disturbing the main project.


Slide 5b:

Branching


* The default branch of the Git is master.
  • We use different branches to develop new modules
  • And it would be merged with the master branch later.


Slide 6:

Branching – Flow diagram


For example, this diagram visualizes a repository with master and new-module branches.


There are some commits named C1, C2 and C3 in the master branch.


Then a branch new-module is created in C3 commit.


C4, C5 and C8 are the commits of new-module branch.


At the same time, the commits C6 and C7 have been made in the master branch.


Here you can see that new-module branch is not disturbing the master branch.


Once the new-module is ready, we will merge it back with the master branch.

In this tutorial, I will demonstrate how branching works.


Merging will be covered in the next tutorial.

Press Ctrl+Alt+T to open the terminal Press Ctrl+Alt+T to open the terminal.
We will open our Git repository mywebpage which we created earlier.
Type cd mywebpage and press Enter Type cd space mywebpage


and press Enter.

I will continue to use html files for demonstration.


You may use any file type of your choice.

Type git log --oneline and press Enter


Let us check the Git log by typing

git space log space hyphen hyphen oneline


and press Enter.

Type git branch and press Enter


Highlight master

First, we will check whether we have any branch in the repository.


Type git space branch


and press Enter.


It shows the default branch master, as mentioned earlier.

Type git branch new-chapter and press Enter Now say, I want to create a branch named new-chapter.


Type git space branch space new-chapter


and press Enter.

Type git branch and press Enter


Highlight new-chapter

Let us see the branch list by typing git space branch and press Enter.


Here we can see the branch new-chapter in the list.

Highlight * master We can also see an asterisk symbol with the master branch.


It indicates that currently we are working in the master branch.

Type git checkout new-chapter and press Enter To go into the new-chapter branch, type git space checkout space new-chapter


and press Enter.

Type git branch and press Enter


Highlight * new-chapter

To check the branch name, type git space branch


and press Enter.


By seeing the asterisk, we can understand that now we are in the new-chapter branch.


<<PAUSE>>

Type gedit story.html & and press Enter Next, I will create a html file story.html and commit it for demonstration purpose.


Type, gedit space story.html space ampersand


and press Enter.

Copy and paste some code I will copy and paste some code into this file, from my Writer document, which I had saved earlier.
Save and close the file Save and close the file.
Remember, we have to commit our work whenever we add or remove any file.
Type git add story.html and press Enter


To add the file to the staging area, type

git space add space story.html


and press Enter.

Type git commit -m “Added story.html in new-chapter branch” and press Enter To commit our work, type

git space commit space hyphen m space within double quotes “Added story.html in new-chapter branch”


and press Enter.

Type git log --oneline and press Enter Let’s check the Git log of new-chapter branch by typing git space log space hyphen hyphen oneline


and press Enter.

Highlight “Added story.html in new-chapter branch Here we can see our latest commitAdded story.html in new-chapter branch”.


<<PAUSE>>

Type git checkout master and press Enter Now, let’s say we want to go back to our master branch to do some work.


So we’ll type, git space checkout space master


and press Enter.

Type git log --oneline and press Enter To check the Git log, type git space log space hyphen hyphen oneline


and press Enter.

Here we can't see the commitAdded story.html in new-chapter branch”.


This is because, that commit belongs to new-chapter branch only.

Type ls and press Enter


Let’s check the folder content by typing ls and press Enter.


Here we can't see the file story.html also.

Type gedit history.html & and press Enter Next, we shall make some changes in the file history.html.


Lets open the file by typing gedit space history.html space ampersand


and press Enter.

Add some lines Let’s add some lines.
Save and close the file Save and close the file.
Type git commit -am “Added chapter two in history.html” and press Enter


Let’s commit our work at this point by typing


git space commit space hyphen am space within double quotes “Added chapter two in history.html”


and press Enter.


Till now, we have been working with the master branch.


<<PAUSE>>

Type git checkout new-chapter and press Enter


Now, let us check if this commit is reflected in new-chapter branch.


To go into new-chapter branch, type

git space checkout space new-chapter


and press Enter.

Type git log --oneline and press Enter Let’s check the Git log by typing git space log space hyphen hyphen oneline


and press Enter

Here, we can't see the commit “Added chapter two in history.html” as that is in the master branch.
Type gedit story.html & and press Enter Let’s add some lines in our file story.html.


Type gedit space story.html space ampersand


and press Enter.

Copy and paste the lines I will add some lines from my Writer document.
Save and close the file Save and close the file.
Type git status and press Enter To check the Git status, type git space status and press Enter.
Note that we have not committed our work at this stage.


What do you think will happen if we try to switch branch without committing?


This should lead to an error.


<<PAUSE>>

Type git checkout master and press Enter Let us try to go back to the master branch.


Type git space checkout space master and press Enter.

Highlight the error

“Please, commit your changes or stash them”

This error shows that we can't switch back to other branches without committing the changes.
But what, if I don't want to commit the changes, as they are not important at this stage?


This can be done using stashing.

Highlight stash We will learn about stashing in the upcoming tutorials.
Type git checkout --force master and press Enter


For now, we will forcefully exit this branch by using the hyphen hyphen force flag.


Type git space checkout space hyphen hyphen force space master


and press Enter.

Once again, we will go back to new-chapter branch, to check whether the changes are discarded or not.
Type git checkout new-chapter and press Enter Type git space checkout space new-chapter


and press Enter.

Type gedit story.html & and press Enter Let’s open the file story.html by typing gedit space story.html space ampersand


and press Enter.

Here we can see that our changes have been discarded.


Let us close the gedit.

In the next tutorial, we will learn to merge this new-chapter branch with the master branch.


<<PAUSE>>

With this, we come to the end of this tutorial.
Slide 7:

Summary


Let us summarize.


In this tutorial, we have learnt about

  • Branching
  • Creating a branch and
  • Switching between the master branch and the new branch


Slide 8:

Assignment


As an assignment
  • Create a branch named chapter-two.
  • Go into the chapter-two branch.
  • Do some commits.
  • Switch back to master branch.
  • Check the Git log and
  • Understand that, in the master branch, you can't see the commits of the branch chapter-two.


Slide 9:

Acknowledgement


The video at the following link summarises the Spoken Tutorial project.


Please download and watch it.

Slide 10:

Spoken Tutorial Workshops


The Spoken Tutorial Project Team conducts workshops and gives certificates to those who pass online tests.


For more details, please write to us.

Slide 11:

Acknowledgement


Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.


More information on this Mission is available at the following link.

This is Priya from IIT Bombay. Thanks for joining.

Contributors and Content Editors

Priyacst