1. Home
  2. DevOps
  3. How to Delete Git Branches on Local and Remote Repositories: A Comprehensive Guide

How to Delete Git Branches on Local and Remote Repositories: A Comprehensive Guide


Git, the version control system, allows us to work on different versions of a project, commonly known as branches. It’s typical practice to remove a branch post its utilization to maintain the repository’s clutter-free environment. This article will guide you through the steps of how to delete Git branches on both local and remote repositories.

Removing a Local Git Branch

Deleting a local Git branch is relatively straightforward. Here’s how you can do so:

  1. First, ensure that you are not currently on the branch you wish to delete because Git prevents you from deleting the currently active branch. You can switch to another branch using the checkout command:
git checkout <another-branch>
  1. Once you’re on a different branch, use the -d flag with the branch command. The -d stands for –delete, which tells Git we want to delete the branch:
git branch -d <branch-to-delete>

If the branch has not been fully merged, Git will give you a warning message. If you are sure that you still want to delete it, use the -D flag instead.

git branch -D <branch-to-delete>

Remember, using -D flag enforce deletion, and any unmerged changes will be lost forever.

Deleting a Remote Git Branch

For remote branches, the process is slightly different. Here are the steps to delete a remote git branch:

  1. You can delete a remote branch using the push command with --delete flag:
git push origin --delete <branch-to-delete>

This command pushes nothing to the remote repository branch, effectively deleting it.

  1. Sometimes, despite removing the branch, it might still show up when listing all the branches with git branch -a. To overcome this, you need to synchronize your local repository with the remote one using the fetch command with -p option:
git fetch -p

The -p option stands for –prune, which removes any remote-tracking links to branches that have been removed on the remote repository.

Deleting branches that are no longer required helps to keep your project clean and manageable. However, always be careful when deleting branches to make sure you do not lose important changes. Happy coding!

If you like our post, please share it: