How to remove Git branches from your repository

Any programmer who practices branch based development strategy will inevitably end up with a stack of leftover branches that aren’t needed anymore. Fortunately, it’s fairly easy to remove Git branches that were created locally.

Remove Git branch command

To remove a Git branch created locally, just issue one of the following two commands:

  • git branch -d name-of-branch-to-remove
  • git branch -delete name-of-branch-to-remove

If you have multiple upstream accounts, you may need to specify the upstream alias in your branch removal command:

  • git branch -delete origin/name-of-branch-to-remove
  • git branch -delete backup/name-of-branch-to-remove
  • git branch -delete sandbox/name-of-branch-to-remove

Remove remote Git tracking branches

Things get a little trickier when a branch originated from a remote repository. If you remove the Git branch locally, there will still be a remote tracking branch in your repository’s list of local branches. Here is the Git command to remove a local tracking branch:

  • git branch --remotes --delete origin/name-of-branch-to-remove
  • git branch -r -d origin/name-of-branch-to-remove

Remove remote Git branch from GitHub or GitLab

Removing the Git branch locally, and removing the remote tracking branch will rid your local development environment of the branch in question, but it will not remove it from any remote repositories such as GitHub, GitLab or BitBucket. To do this, you must issue the following Git command to remove a branch from a remote repository:

  • git push origin --delete name-of-branch-to-remove

The nice thing about this command is that it will not only remove the remote Git branch in the GitHub or GitLab repository, but it will also remove the locally configured remote tracking branch as well. However, this command will not remove the local branch with the same name. The local Git branch remove command must be issued separately.

If you do attempt to remove a GitHub or GitLab branch, ensure you have the appropriate rights to do so, and also ensure there are no branch protection rules that forbid the removal of the branch in question. Otherwise, your attempt to remove a Git branch remotely will fail.

Remove Git branch on GitHub or GitLab

The online interface for tools like GitHub or GitLab make it possible to remove a branch by simply clicking a trash can icon next to the branch’s name. In this case, the branch will be deleted from the central repository, but the remote tracking branch and the branch itself will still exist in any existing clone of that repository.

To locally remove a remote tracking and bring your local repository in sync with the server, just issue a git fetch command with the prune option:

  • git fetch origin --prune

After this command runs, the remote tracking branch will be removed from your local environment. An additional removal of the local instance of the Git branch will be required as well.

Steps to remove Git branches

To ensure you have successfully removed a Git branch, make sure these steps have been followed:

  1. The Git branch has been removed from the remote repository such as GitHub, GitLab or BitBucket
  2. The remote tracking branch has been removed from the local developer’s repository
  3. The developer has removed the local Git branch from their workstation

With these steps completed, the Git branch will have been successfully removed.

App Architecture
Software Quality
Cloud Computing