Git

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. Create a Git Repository

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

First, we shall create a directory for the master branch.

Create directory for initializing Git branch
Create a directory for initializing the Git branch

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

Initialize Git repository
Initialize Git repository

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

Add and commit file to repository
Add and commit the file to the repository

Now 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 a directory for a new branch

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

Clone master branch
Clone master branch

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

Create new branch
Create new branch

3. Git delete local branch in Action

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 the command git branch -d <branch name>.

Delete local branch
Delete local branch

3.1 -d / -D option

The -d option will delete the branch only if it has already been pushed and merged with the remote branch. We can also use -D option (in Uppercase) if we want to forcefully delete the branch. -d is a shortcut of --delete. -D is a shortcut of --delete --force

Let’s create a new branch:

~/study/github/MockRestServiceServerExample$ git branch test-branch

Test branch has been created:

~/study/github/MockRestServiceServerExample$ git branch
* master
  test-branch

Switch to the new branch:

~/study/github/MockRestServiceServerExample$ git checkout test-branch
Switched to branch 'test-branch'

Make a modification and commit the changes to the local branch. Don’t push the changes in the remote repository:

~/study/github/MockRestServiceServerExample$ git commit -am "test branch deletion"
[test-branch 93b8f82] test branch deletion
 1 file changed, 1 insertion(+), 1 deletion(-)

Switch to a different branch:

~/study/github/MockRestServiceServerExample$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

Now try to delete the previous branch with a -d option:

~/study/github/MockRestServiceServerExample$ git branch -d test-branch
error: The branch 'test-branch' is not fully merged.
If you are sure you want to delete it, run 'git branch -D test-branch'.

As we can see we can’t delete the branch because there are local changes in the local branch which are not pushed in the remote repository. Now try to delete the branch using a -D option.

~/study/github/MockRestServiceServerExample$ git branch -D test-branch
Deleted branch test-branch (was 93b8f82).

We can see that the branch gets deleted.

-d and _d options

4. Conclusion

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

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.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button