Many git workflows deal with both long-term and temporary branches. Therefore, there is often a need to delete branches during development. There is occasionally a need to delete shared branches, from a remote server, as well as local branches.

Why Delete a Branch?

First, if you’re still getting to grips with git, there’s a pretty good chance you’ll create a branch and then decide you didn’t need to. Or you might be experimenting with branches and wanting to clear up after yourself. This is fine since branching in git 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 branch for a single bug fix. This is true even if it involves just a single author making a one-line change in a single file.

For these reasons, creating and deleting branches are operations that need to be well understood. You might find yourself often deleting branches during a typical development workflow.

A Sample Repository With Branches

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

Note that each local branch has a corresponding upstream branch from the remote: origin.

Deleting a Branch Using the Command Line

The basic command syntax for deleting a branch is:

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

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

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

If you delete a branch that only exists locally, with unmerged changes, you’ll lose those changes. Therefore, git will refuse to delete a branch in such a situation, by default:

As the error message informs, you can force deletion with the -D flag. However, git will allow you to delete an unmerged local branch if it exists remotely:

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:

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:

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:

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 :

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:

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:

Deleting Local and Remote Branches Using Tower

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

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

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. The option simply does not appear. Deleting a branch is straightforward, though. From the repository’s Code page, click the branches link, locate the branch to delete, then click the Delete this branch icon, which looks like a trash can:

Be aware that there are no checks for unmerged changes, so on GitHub, the branch will simply be deleted immediately. However, since it will always represent a remote branch, this should be the behavior you’re expecting.

Note that, after deleting, you’ll see a button to Restore the branch. However, this is simply a useful undo feature, 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!

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:

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

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.