Key Takeaways

  • Deleting branches is an important part of a typical Git workflow.
  • You can delete a local branch using the command "git branch -d [branchname]", but be cautious of unmerged changes.
  • Different tools like GitHub Desktop, GitKraken, Tower, and Bitbucket have their own methods for deleting branches, so familiarize yourself with the specific process for each tool.

Git’s branches are so lightweight that it’s easy to create them whenever you need to. But it’s also easy to end up with leftover branches you no longer need. So what’s involved in deleting a branch?

Why Delete a Branch?

First, if you’re still learning how to structure a project using git branches, there’s a pretty good chance you’ll create a branch and then decide you didn’t need to. This is fine since branching is a lightweight operation; it’s very fast and uses disk space efficiently.

As a result, many git development workflows encourage branching, even for very small or short tasks. For example, a common strategy is to create a separate branch for each and every bug fix.

Deleting branches should be just as much a part of your everyday workflow as creating them is.

A Sample Repository With Branches

The examples below refer to a sample repository with the following structure:

A list of git branches showing two local and three remote

Note that the repo has two branches: main and dev. Each of these local branches is a tracking branch, with a corresponding upstream branch on a remote server (origin).

It’s easy to lose track of branches, so make sure you know how to list git branches from the command line or view them in your preferred client.

Deleting a Branch Using the Command Line

The basic command syntax for deleting a branch is:

        git branch (-d | -D) [-r] <branchname>...
    

How to Delete a Local Branch

The simplest form of the command deletes a local branch, providing all its changes have been merged:

        git branch -d dev
    

When things go right, you’ll see a confirmation message:

A command line showing the removal of a git branch using 'git branch -d'

You can’t delete the branch that is currently active; if you try to do so, you’ll get a message like this:

A git error message explaining that the main branch cannot be deleted

If you delete a local branch with unmerged, unpushed changes, you’ll lose those changes. Therefore, git will refuse to delete such a branch by default. In this example, dev has unmerged changes that have not been pushed to the remote branch:

A git error message explaining that an unmerged branch cannot be deleted

As the error message says, you can force deletion with the -D flag.

You can delete a local tracking branch if its corresponding remote branch is up-to-date, even if it's unmerged. Git will still show a warning, so you’re aware of what’s happened:

A git error message explaining that the deleted branch has been merged to the remote, but not HEAD.

How to Delete a Remote Branch

Deleting a remote branch is quite different. You’ll use the git push command along with the -d flag to delete. After that, supply the name of the remote (often origin) and the branch name:

The git command line removing a remote branch.

Deleting Local and Remote Branches With GitHub Desktop

Unlike the command-line git program, GitHub’s desktop app will only let you delete the active branch. You can carry out this action via the Branch menu, by selecting the Delete option and confirming it:

A screenshot showing how to delete a branch using GitHub desktop

GitHub Desktop won’t let you delete the default branch—e.g. main—even though git itself supports this. If the default branch is the one that is currently active, the app disables the menu action.

If the branch also represents a remote branch, GitHub Desktop gives the option of deleting it from the remote too:

A screenshot showing a remote branch being deleted in GitHub Desktop

Deleting Branches Using GitKraken

GitKraken displays your repository’s local and remote branches in the left-hand sidebar. You must delete each separately.

Hover over the appropriate branch name and click the Branch actions menu which looks like three vertical dots. From the menu, select Delete <branch name>:

Deleting a local branch using GitKraken

You’ll see a confirmation message informing you that this is a destructive operation. You can confirm you want to continue with the Delete button:

A screenshot of GitKraken showing a warning when attempting to delete a branch

Reflecting the default behavior of the git command-line program, you must first switch to a branch other than the one you’re deleting. Otherwise, you’ll see an error message:

A screenshot of GitKraken showing an error when attempting to delete the current branch

Deleting Local and Remote Branches Using Tower

Deleting a branch with Tower is very similar to deleting a branch with GitKraken. The app displays local and remote branches in a panel on the left-hand side. Right-click on any branch and select the Delete option from the context menu:

A screenshot showing how to delete a local branch in Tower

One key difference is that you can delete a remote branch, along with its local branch, during confirmation:

A screenshot showing the confirm dialog in Tower when deleting a branch

Deleting a Branch on GitHub

GitHub only acts as a remote source, so branches there are remote by default. If you delete a branch using the GitHub website, you’ll have to delete the corresponding local branch using one of the other methods here.

As with the GitHub Desktop app, the GitHub website will not allow you to delete the default branch. If you try, you’ll see an error message:

A GitHub error message explaining that you cannot delete the default branch.

Deleting any other branch, however, is straightforward. From the repository’s Code page, click the branches link, locate the branch to delete, then click the Delete branch icon, which looks like a trash can:

A screenshot of the Branches page on GitHub

Be aware that there are no checks for unmerged changes, so GitHub will simply delete the branch immediately. However, since it will always represent a remote branch, this behavior should make sense.

Note that, after deleting, you’ll see a button to Restore the branch. However, this is simply a useful undo feature, just in case you click the delete icon accidentally. Do not rely on it, because as soon as you refresh or navigate away from the page, you’ll lose the option!

A screenshot of GitHub showing the effects of a branch having been deleted

Deleting Local and Remote Branches on Bitbucket

Bitbucket, like GitHub, will not allow you to delete the default branch. Bitbucket calls this the Main branch in Repository settings. You can delete any other branch listed on the Branches tab, via its corresponding Actions menu:

A screenshot showing a Branch Actions menu on Bitbucket

You can also delete more than one branch at once if you’re doing a big cleanup operation:

A screenshot showing multiple branches being deleted on Bitbucket

Deleting Branches Is Part of a Typical Git Workflow

Git branches can complicate your workflow, especially one with local, remote, and tracking branches. But for simple day-to-day development, you’re likely to be creating and deleting local branches all the time. This is a core aspect of a typical git workflow you should become accustomed to.

If you find it difficult to remember exactly how to delete a branch, consider setting up aliases to remove branches and simply the process.