Git/C2/Merging-and-Deleting-branches/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Merging and deleting branches

Author: Priya K

Keywords: Video tutorial, git, branch, merging, deleting, revert merging


Visual cue
Narration
Slide 1:

Merging and deleting branches

Welcome to the spoken tutorial on Merging and deleting branches.
Slide 2:Learning Objectives


In this tutorial, we will learn about
  • Merging
  • Revert merging and
  • Deleting 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 basics of Git commands and branching in Git.
  • If not, for relevant Linux tutorials, please visit our website.
Earlier in the series, we learnt to create branches.


Now, we will learn how to merge two branches.

Slide 5:

Merging diagram


This diagram illustrates how the new-module branch is merged with the master branch.


This is done in the C9 commit.

Slide 6:

After Merging


After merging, the commits of new-module are added to master branch.


Let me explain how it works.

Press Ctrl+Alt+T to open the terminal First, we will open our Git repository mywebpage which we created earlier.


Press Ctrl+Alt+T to open the terminal.

Type cd mywebpage and press Enter To go into our Git repository, 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.

From here onwards, please remember to press the Enter key after typing every command on the terminal.
Type git branch and press Enter


Highlight master and new-chapter

Let us check the branch list by typing git space branch.


We can see that we have two branches named master and new-chapter.

Highlight new-chapter >> Highlight master The new-chapter branch was created earlier in this series and master is the default branch.
Highlight * master Currently, we are in the master branch.
Type git log --oneline and press Enter We will check the Git log by typing git space log space hyphen hyphen oneline
Type git checkout new-chapter and press Enter Let’s go to new-chapter branch and check the Git log.


Type git space checkout space new-chapter

Type git log --oneline and press Enter Type git space log space hyphen hyphen oneline
Now we will compare the commits of master and new-chapter branches.
Highlight the first 4 commits of master and new-chapter branch These four commits are common to both the branches.
Highlight Added story.html in new-chapter branch


Highlight Added chapter two in history.html

"Added story.html in new-chapter branch" is in new-chapter branch.


And "Added chapter two in history.html" is in master branch.

After merging, “Added story.html in new-chapter branch” commit will be added in the master branch.


<<PAUSE>>

Type git merge master and press Enter Let me demonstrate how to merge now.


Type git space merge space master

Gedit opens up automatically to receive the commit message.


Recall that we have configured gedit as the core editor of Git.


If you had configured another editor, then that one will open up.


If you are using a Git version that is below 1.9, then the editor may not open.


So, you may skip the next step.

Highlight the first line of gedit


Save and close the gedit

I use the default commit message as it is.


If you want to give any other message related to the merging, then type it here.


Now save and close the editor.

Type git log --oneline and press Enter We will check the Git log again.
Highlight the commits You can see that the commits of master branch are merged with new-chapter branch.
Highlight the merge commit message You can also see a commit message for the merging.
Type git checkout master and press Enter Next, we will go to master branch and check the commits.


Type git space checkout space master

Type git log --oneline and press Enter Let’s check the Git log.


Here we should have seen master branch commits along with new-chapter commits.


But the Git log shows master branch commits only.

Highlight the Git log Ideally, we should have merged the new-chapter branch to the master branch.


But we have merged it the other way around.


That’s why we can't see the merging commit in the master branch.


<<PAUSE>>

So, how can we revert this merging?
For this, we will have to go back to the new-chapter branch.
Type git space checkout space new-chapter Type git space checkout space new-chapter
Type git reset --hard HEAD~ and press Enter To revert the merge, type git space reset space hyphen hyphen hard space HEAD tilde
Highlight HEAD~ Recall that latest revision is always HEAD and latest minus 1 revision is always HEAD tilde.


So we have used HEAD tilde to get the previous revision of merging.

Type git log --oneline and press Enter Let’s check the Git log once again.
We can see that the merging is discarded now.


<<PAUSE>>

We will now merge the new-chapter branch to the master branch.
Type git checkout master and press Enter We will go to master branch first by typing git space checkout space master
Type git log --oneline and press Enter Let’s check the Git log again.
Type git merge new-chapter and press Enter


Close the editor

To merge, we will type git space merge space new-chapter


Give your merging commit message in the gedit.


Then save and close the editor.

Type git log --oneline and press Enter Again check the Git log.
We can see that our new-chapter branch has been successfully merged to the master branch.
Type git merge new-chapter and press Enter Let’s try to merge it again.


Type git space merge space new-chapter

Highlight “Already up-to-date” Now we see a message that says “Already up-to-date”.


This is a good way to verify whether we merged or not.


<<PAUSE>>

After merging, the new-chapter branch can be deleted from the Git repository.
Type git branch -d new-chapter and press Enter To delete the branch, type git space branch space hyphen d space new-chapter
Type git branch and press Enter Let’s check the branch list once again by typing git space branch


We can't see new-chapter branch anymore as it has been deleted.

Highlight -d in the deleting command To delete a branch without merging,
  • use hyphen D in uppercase
  • instead of hyphen d in lowercase.


<<PAUSE>>

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

Summary


Let us summarize.


In this tutorial, we have learnt about

  • Merging
  • Revert merging and
  • Deleting branches
Slide 8:

Assignment

  • Check the commits of the branch chapter-two which you created in the previous assignment
  • Merge it to the master branch
  • Delete the chapter-two branch


As an assignment
  • Check the commits of the branch chapter-two which you created in the previous assignment
  • Merge it with the master branch and
  • Delete the chapter-two branch
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

Nancyvarkey, Priyacst