Tutorial 8 : Create branch

What you will Learn:

Branches overview

A branch represents an independent line of development.

To understand it better, you can see ‘4 commits’ link below

Create branch

Click this link. As you can see below, each commit is represented by a hash number. The numbers are the hashes for each commit

Create branch

Create branch

All these commits are inside a main branch or master branch. Main (or master) branch is the default branch. The hash numbers can also be seen in the ‘git log’ command, see above.

So the main branch can be represented as below. The left hand side shows the respective commit messages

Create branch

Now, if 2 developers are working on the same project, each developer will create a branch for the respective feature they will be working on and then they start their development work. Once the developer is done with their respective feature, they will merge their branch with the main branch, see below

Create branch

If you want to know the branch name you are working on in your local machine, type ‘git branch’

Create branch

The asterisk * represents the current branch we are in.

If you want to know the branch name in remote GitHub repository, type ‘git branch -r’

Create branch

To know the name of all the branches, type ‘git branch -a’

Create branch

Notice that the red color in previous 2 commands signifies the branch on remote GitHub. The asterisk * represents the current branch we are in

Also Check: Selenium Python Training

Create new branch

To create new branch, we take a copy of existing branch and then you can make any changes in your branch without affecting main branch. So we use ‘git branch <feature-name>’ syntax to create a branch

Create branch

Now if we execute ‘git branch’ we will see our new branch name

Create branch

Switch to a new branch

Before we make any changes to our new branch, we have to first switch to it. Use command syntax ‘git checkout <branch-name>’ to switch to a desired branch. After executing the command, notice that we see (feature-landingpage) in the next command line, see below

Create branch

Switch to main branch

Simply execute ‘git checkout -‘ to switch to master branch

Create branch

To switch back to new branch, you can again execute the same command

Create branch

Create a new file on the new branch

Create a new file under this new branch

Create branch

As expected, the ‘git status’ command shows the untracked file

Create branch

Let us add and commit the file

Create branch

The ‘git log’ will reflect the above commit as its last commit

Create branch

This branch is not yet on GitHub, see below

Create branch

Also Check: Selenium Training Online

Push branch to GitHub

Let us push this new branch to GitHub. Just execute ‘git push’. You will get the exact command you need to run

Create branch

You can either use the above highlighted command OR you can also use ‘git push -u origin feature-landingpage’.

Type username and password, you can now see the new branch gets pushed to GitHub

Create branch

Refresh the GitHub, you can now see 2 branches

Create branch

Select the branch from the above dropdown, you can see the new file, see below

Create branch

However if you switch back to main branch, you will not see this landing html file

Create branch

Thank you for reading!

Also Read: Tutorial 6: Push changes to GitHub

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

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

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024