How To Delete a Branch in Git

As you will likely recall from our introductory guide on everything Git, you can create discrete branches in your project, upon which you can make changes, test things, and work non-destructively, always reserving the option to “check out” other branches. The branch functionality is one of the most important systems you can master with Git, and it will help you manage your projects with maximum efficiency. But over time, your branches might start to add up. Some of these branches will get merged into the “master” branch while others may rot on the vein and never be used. In this article, you’ll learn how to delete branches you no longer need.

Basic Options

There are two important options for you to memorize when it comes to deleting branches in Git:

-d
Delete a branch, followed by the name of the branch.
-D
Force deletion of a branch.

Below, you will see these options in action. Circumstances will dictate which one you choose to use, but at this time it is a good idea to commit them to memory. An easy mnemonic clue for this option is to remember that when it comes to Git branches, “D” stands for delete.

The basic usage of the command is as follows:

git branch -d <name of branch>

But as you will see below, this operation may trigger a helpful error. Git will try to prevent you from deleting content you might need. Read on below to find out how.

Deleting a Git Branch in Practice

The -d option will not work if you are trying to delete the same branch you are currently checking out. Remember, “checking out” a branch means you are currently working on that branch.

Git will not allow you to delete a branch on which you are currently working.

In this instance, imagine you are working on a branch called “changes”. If you try to delete the “changes” branch with the -d option, while the branch is stilled checked out, you will receive this error:

error: Cannot delete branch 'changes'

You are seeing this error because you are trying to delete an active branch while it is checked out.

In order to properly delete the “changes” branch you will need to check out another branch. For example, you would need to check out the “master” branch and then attempt to delete “changes”. First, check out “master”:

git checkout master

If you run the git branch command, you will see that “master” is now selected as the active branch. Try to delete “changes” now with the -d option and see what happens:

git branch -d changes

You will receive another helpful error:

error: The branch 'changes' is not fully merged.
If you are sure you want to delete it, run 'git branch -D changes'.

This error indicates that the “changes” branch has content and modifications that have been not been merged, therefore these changes would be lost if you delete the branch. The second line informs you that if you wish to proceed with the deletion you can use the -D option to “force” a deletion.

git branch -D changes

Which is equivalent to:

git branch --delete --force changes

You will see a success message indicating that you deleted the branch:

Deleted branch changes (was 3527e57).

Well done! You now know how to delete a branch from your Git project. As always, be very careful when deleting any content you might need, your deleted will be unrecoverable.

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+