Git Branching and Merging with Master

In this article, We are going to learn the basics of Git and Git Branching and Merging with Master with an example.

What is Git?

Git is a fast, scalable, distributed revision control system with a rich command set that provides both high-level operations and full access to internals.

Below are the some of the important terminology of Git.

  • Branch
  • Checkout
  • Cherry-picking
  • Clone
  • Fetch
  • Fork
  • HEAD
  • Index
  • Master
  • Merge
  • Origin
  • Pull/Pull Request
  • Push
  • Rebase
  • Remote
  • Repository
  • Stash
  • Tag
  • Upstream

In this topic mainly we are concentrating on the Git Branching and Merging with Master.

What is git branching?

Branches can be a new version of a repository, experimental changes, or personal forks of a repository for users to alter and test changes. By default, Git has a master branch, which is same as the trunk in Subversion. 

Most of the time branch is created for developing new features. Once the feature is completed, it is merged back with the master branch and we delete the branch.

Every branch is referenced by HEAD, which points to the latest commit in the branch. Whenever you make a commit, HEAD is updated with the latest commit.

Git Branching and Merging with Master

Create a new directory and initialize a Git repository. We are going to create a directory named “learning”. below is a command for initializing a Git repository and adding a file into the repository.

Create a new file named “hello.txt” in the “learning” directory and commit.

Right now we are in the master branch. If we push anything in here it always stored into the master repository.

Create a new Branch:

Create a new branch as “Test” by executing below command.

to executing below command we can see a list of branch created and the active branch.

Git Branching
Git Branching

Switching Branches:

Executing the following will point HEAD to the tip of <branchname>. In Git terms, a “checkout” is the act of switching between different versions of a target entity. The git checkout command lets you navigate between the branches created by git branch.

Git branch switching Example
Git branch switching Example

Now you are on the “Test” branch, you can start adding commits to it.

Git Branching and Merging with Master
Git Branching and Merging with Master

At this point, We have two branches such as master and test branch and each branch contain different files. Below image shows the different types of branch and development of it.

 

Git Branching and Merging with Master
Git Branching and Merging with Master

Now the code is there in two different line of development. We have to integrate with a single branch using git merge.

Git Merge:

As discussed above git merge command used to merge the two different branches. Git merge will combine multiple sequences of commits into one unified history.

How does git Merge Works?

In the above scenarios, git merge takes two commit pointers, usually the branch tips, and will find a common base commit between them. Once Git finds a common base commit it will create a new “merge commit” that combines the changes of each queued merge commit sequence.

As we can see in the below image, the test branch is merged with the master branch and created a common base branch of it.

Merged tip
Merged tip

By executing below command we can verify only one file exists in the master branch.

Git Branching and Merging with Master
Git Branching and Merging with Master

To merge commits into the master branch, let’s now switch over to the master branch.

Use the merge command to merge branches. By running the command below, the specified commit will be merged to the currently active branch.

Git Branching and Merging with Master
Git Branching and Merging with Master

Now run below command to verify the specified commit will be merged to the currently active branch.

Git Branching and Merging with Master
Git Branching and Merging with Master

Summary:

In this article, we have discussed basic of git and important terminology of Git. In-depth we have explained about git branching and merging along with a simple example. 

This document integrated and referenced other Git commands like: git branch, git add, git list, git checkout and git merge.

In the next article, We are going to discuss how to resolve git conflict and more about git merging.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons