Tutorial 3 : — Dealing with branches in Git

Gaurav Patil
2 min readJul 23, 2022

--

Photo by Gabriel Heinzer on Unsplash

Missed previous tutorial ? — click here.

What is a branch in Git ?

Since Git is a version control system, branch is a separate version of a repository. Whatever operations we have performed in previous tutorials are done in master(main) branch of the repository.

We will cover following topics in this tutorial :

Step 1) Creating a new branch

Step 2) Listing all the branches

Step 3) Switching between branches

Step 4) Deleting a local branch

Step 5) Checking the history of changes in repository

We have already created a trial folder and stored sample.html file in it in our previous tutorial.

Step 1) Creating a new branch

Run following command in command prompt:

git checkout -b first_branch

where first_branch is the name of branch we are creating.

Output :

Switched to a new branch ‘first_branch’

Thus we have created a new branch and we moved from master branch to newly created first_branch.

Step 2) Listing all the branches

Run following command in command prompt:

git branch

Output :

* first_branch
master

It says, there are two branches in total viz. first_branch and master branch. Asterisk(*) before first_branch indicates our current branch .

Step 3) Switching between branches

Run following command in command prompt:

git checkout master

where ‘master’ is a branch name to switch in.

Output :

Switched to branch ‘master’

Let’s see if we’re in master branch :

git branch

Output :

first_branch
* master

Now asterisk(*) is present before master branch indicating it is our current branch. So we successfully switched from first_branch to master branch

Step 4) Deleting a local branch

git branch -d first_branch

where first_branch is a branch name to be deleted.

Output :

Deleted branch first_branch (was fb91519).

You can again check the branches by running “git branch” command .

Note:

git checkout -b <branch name> is a shortcut for git branch <branch name> followed by a git checkout <branch name>.

Step 5) Checking the history of changes in repository

Run following command in command prompt:

git log

This will enlist all the changes you made in the repository as output in a command prompt.

Congratulations !! Now you know all the operations performed on a branch in a Git repository.

But what if I want someone’s repository in my PC and then make some changes ? How do we do it and what is cloning ? Take a deep breath and move to the next tutorial .

--

--

Gaurav Patil
Gaurav Patil

Written by Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.

No responses yet