How to remove branches in Git

In this tutorial, I will explain how to remove Git branches locally as well as in the remote server. First, let us have a look at the general way of deleting branches both locally and remotely.

The command for removing a branch locally:

$ git branch –d <branch-name>

Suppose, the branch name we want to delete is tst-br. The command for removing this branch:

$ git branch –d tst-br

If Git Bash displays the message:

Error: The branch “tst-br” is not fully merged …

Git delete unmerge

Then you may delete this by -D flag as shown below:

$ git branch -D tst-br

As such, -d flag is the alias for –delete, it requires you to fully merge the branch to be deleted to its upstream branch.

If you are want to remove this even branch is not fully merged then use the -D flag which is the alias for force delete i.e. –delete –force.

How to fully merge the branch?

Before running the command with -d flag, you may use this command for fully merging with upstream branch:

Suppose, our branch name that is not fully merged is hello-git-branch.  To merge this branch, run this command after checking out into master branch i.e.:

$ git checkout master

Then run the merge command:

$ git merge hello-git-branch

Git delete merge

If this branch has an upstream branch then execute these two commands:

$ git checkout hello-git-branch

$ push

And finally, for deleting the hello-git-branch with –d flag, execute these two commands:

$ git checkout master

$ branch -d hello-git-branch

How to undo removal operation of a branch?

If you accidentally removed a branch or just testing and want to undo the branch removal operation then this section describes how you may do this.

Suppose we want to recover the same branch as I deleted in above section i.e. hello-git-branch.

For recovering a branch, you need SHA1. The SHA1 code can be seen as you deleted a file (see picture below):

Git delete undo

Take this code and create a new branch. For example:

$ git branch hello-git-branch 7f5faa4

(The SHA1 7f5faa4 will vary, so take the code from your Git Bash).

This command should create the branch from the same point where this was removed.

The SHA1 code mentioned above can be found immediately after you removed a branch and want to recover the branch. However, if you cleared the screen or restarted the Git Bash then this code won’t be visible.

You may learn more about getting the SHA1 here.

How to delete branches in Github (remotely)?

For deleting a branch from your remote repository (in this case Github), you may use the –delete. For the demo, we have a test repository on Github website. The repository contains two branches – one is the master while the other is hello-git-branch.

Git delete remote

See how this branch is removed by this command:

git push origin –delete hello-git-branch

This command should remove the branch and as I refreshed the repo page in Github website, this is the result:

Git remove branch remote

A step by step guide from creation to deleting a branch

For beginners, this section describes the process of creating a branch locally, adding a file, committing, adding that branch remotely and finally deleting the branch from local and remote repositories.

For that, consider we have a repository with the name tst-demo on Github (remotely) and on the local system.

The first step is creating a branch by this command namely br-tst-1.

$ git branch br-tst-1

A branch should be created in the local repo.

Now see what this and master branch contains by using the ls command:

Git branch create

You can see, both branches contain the same files at this stage.

Adding a new file in br-tst-1 branch

In our repository folder in the local system, I placed a new file file-4.txt and this is how it is added in the br-tst-1 branch:

$ git add file-4.txt

Now commit the file by this command:

$ git commit -m “added file-4 for testing”

This should display the following message:

Git remove commit

Just to remind, we are on the br-tst-1 branch by using checkout command.

Comparing the files in master and br-tst-1 branch

Again run the ls command for both branches after running the checkout command and compare the files:

Git delete files

The above graphic shows that the master branch contains four files while our new branch br-tst-1 contains five files.

Creating the branch on the remote server

Now, it is time to create this branch on our remote repository i.e. Github server.

For seeing or making sure where Git Bash will create this branch in the remote repository, you may run this command:

$ git remote –v

Git check repos

This should list current active remote repository where our new branch will be created (as shown in above graphic).

Create the branch remotely

$ git push origin br-tst-1

This should create the branch remotely as shown in the graphic below:

Git remote branches

Delete the branch locally

Back to our initial topic, deleting the Git branch: this command should remove the br-tst-1 branch from the local repo.

$ git branch -D br-tst-1

This command should delete the branch from the local repo even it is not fully merged. As mentioned in the initial part, you may use the –d flag that will display the error message if this branch is not fully merged.

Removing this branch from the remote repository:

Run this command:

$ git branch -D br-tst-1

The screenshot below shows the deletion message from the remote repo:

Git delete remote

If you refresh the repository online, it should not display that branch anymore.

Author - Atiq Zia

Atiq is the writer at, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!