How to Delete a Git Branch Locally and Remotely?

A Branch is a simple pointer to one of the commits in the version history of our project. Commits follow a parent-child relationship, so we can say that a Branch is a series of commits.

A Git Branch can exist in our local repository or the remote repository hosted on platforms like GitHub. Let's learn how to delete a Git branch locally and remotely.

Deleting a Local Git Branch

A local branch is a branch that exists only on our local machine. We can view these branches by using the Git Branch command.

git branch

We can use the git branch command with the -d flag to delete a local branch. The -d flag is short for --delete. We can use -d to delete a branch that has merged in its upstream branch.

git branch -d <branch-name>
git branch --delete <branch-name>

However, if we want to delete an unmerged Branch, we can use the -D flag. It is short for --delete --force. Git will not allow us to delete an unmerged branch, but we can forcefully delete these branches by using -D.

git branch -D <branch-name>
git branch --delete --force <branch-name>

Note that we cannot delete the branch we are currently on. Use the Git Checkout command to move to a different Branch before deleting the currently checked-out one.

git checkout master     # checking out the master branch
git branch -d feature   # deleting the feature branch

Deleting a Git Remote Branch

A remote branch exists on a remote repository. Deleting a Remote Branch is pretty straightforward. We will use the Git Push command with the --delete option to delete a Remote Branch. We can also replace --delete with -d. We also need to mention the remote name. In most cases, the remote name is the origin.

git push <remote-name> --delete <remote-branch-name>
git push <remote-name> -d <remote-branch-name>

Note that the --delete option is not supported in older versions of Git(older than 1.7.0). We can use the following command instead.

git push <remote-name> :<remote-branch-name>

Deleting a Git Remote Tracking Branch

A remote-tracking Branch is a local branch that tracks the changes made to a remote branch. Their name is of the format remote-name/branch-name(for example, origin/master or origin/feature). We can view these remote-tracking branches by using the following command.

git branch -r

Some remote-tracking branches may be left behind in our local repository when we remove their remote branches from the remote repositories. These remote-tracking branches do not serve any purpose as there is no remote branch to track. We can delete them using the Git Fetch command with the --prune option(or -p).

git fetch <remote-name> --prune
git fetch <remote-name> -p

We can also directly delete a remote-tracking branch by using the following command. We use the --delete and --remotes options with the Git Branch command to do so. In this case, the remote branch still exists in the remote repository. But the remote-tracking branch on our local repository is deleted.

git branch --delete --remotes <remote-name>/<branch-name>
git branch -dr <remote-name>/<branch-name> # -dr short for --delete --remotes


Branching in Git is of great importance. It allows individual contributors to start from existing commits and build something on top of them. Any new feature is developed by creating a new "feature" branch and adding commits to that branch. The feature branch is finally merged into the main or master branch. We can use the Git Branch command with -d or -D flags to delete local branches. Use the Git Push command with the -d flag to delete remote branches.