Home » Software Development » Git » Git Delete Local Branch Example

About Saurabh Arora

Saurabh Arora
Saurabh graduated with an engineering degree in Information Technology from YMCA Institute of Engineering, India. He is SCJP, OCWCD certified and currently working as Technical Lead with one of the biggest service based firms and is involved in projects extensively using Java and JEE technologies. He has worked in E-Commerce, Banking and Telecom domain.

Git Delete Local Branch Example

In this example, we shall learn how to delete Git local branch. To delete a branch, it must be fully merged in its upstream branch, or in HEAD if no upstream was set.

1. A brief on Git branch

A branch represents an independent line of development. These serve as an abstraction for the edit, stage, or commit process.

The git branch command lets you create, list, rename, and delete branches. git branch is tightly integrated with the git checkout and git merge commands.

Usage of git branch command:

git branch [--color[=] | --no-color] [-r | -a]
	[--list] [-v [--abbrev= | --no-abbrev]]
	[--column[=] | --no-column]
	[(--merged | --no-merged | --contains) []] [--sort=]
	[--points-at ] […]
git branch [--set-upstream | --track | --no-track] [-l] [-f]  []
git branch (--set-upstream-to= | -u ) []
git branch --unset-upstream []
git branch (-m | -M) [] 
git branch (-d | -D) [-r] …
git branch --edit-description []

2. Git delete local branch in Action

To see our example in action, we shall start with creating git repository.

First we shall create a directory for master branch.

Create directory for initializing Git branch

Create directory for initializing Git branch

Here, we shall initialize git repository using git init command.

Initialize Git repository

Initialize Git repository

Once we have initialized Git repository, we shall add some file and then add and commit using git add and git commit command respectively.

Add and commit file to repository

Add and commit file to repository

Now we that we have created a master branch, now we shall be creating a directory where we will create a new local branch.

Create directory for new branch

Create directory for new branch

In this directory, we shall clone git master branch that we just created using command git clone.

Clone master branch

Clone master branch

Here, let’s create a new branch ReleaseBranch using command git branch <branch name>. Available branches can be viewed using command git branch.

Create new branch

Create new branch

Now assume that all the work with the local branch is now complete and we now want to delete the same. For this purpose, we will use command git branch -d <branch name>.

Delete local branch

Delete local branch

We can also use -D option (in Uppercase) if we want to forcefully delete the branch.

3. Conclusion

In this example, we learnt the usage of git branch command to delete local branch.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of