Delete a local Git branch

How to delete a local Git branch

Whether you use GitFlow, GitHub Flow or any other branch driven development strategy, you inevitably end up with a local Git repository filled with branches you no longer need.

That’s why it’s good to know how to delete local Git branches permanently. You don’t want hundreds of local branches polluting your development environment.

It’s a single, straightforward command to delete a single Git branch locally:

git branch --delete <branchname>

Steps to delete a local Git branch

To delete a local branch in Git, follow these steps:

  1. Open a Git BASH or a command prompt in the root of your Git repository.
  2. If necessary, use the git switch or checkout command to move off the branch you wish to delete.
  3. Issue the following command:
    git branch --delete <branchname>
  4. Run the git branch -a command to verify the local Git branch is deleted.

Git Error: Cannot delete branch

One rule of local Git branch deletion is that you cannot delete a branch that is currently checked out.

If you try to delete a local Git branch while it is currently checked out , you run into the “Cannot delete branch” error, as seen below:

git@DELETE /c/local/branch (main)
$ git branch -a
* main
new-branch
old-branch
git@DELETE /c/local/branch (main)
$ git branch --delete main
error: Cannot delete branch 'main' checked out at 'C:/git/delete'

Switch before you delete

In the above example, the user tried to delete the main Git branch while the it was checked out, which caused an error.

However, deletion of the local Git branches named new-branch or old-branch would succeed, as those branches are not in a checked-out state:

git@DELETE /c/local/branch (main)
$ git branch --delete old-branch
Deleted branch old-branch (was 44a55a1).

Delete local Git branch command

The command to delete a local git branch can take one of the following two forms:

  • git branch --delete old-branch
  • git branch -d old-branch

The only difference between the, is that the second local branch delete Git command uses an abbreviated syntax. Both commands do the exact same thing.

Remote vs local Git branch deletes

Be advised that when you delete a local Git branch, the corresponding remote branch in a repository like GitHub or GitLab remains alive and active.

You must take further steps if the goal is to delete both local and remote branches.

git no upstream branch error fix

When you delete a local Git branch, the deletion will not be reflected in remote repos like GitHub or GitLab.

Remote Git branch deletion

To remove a remote Git branch in a repository such as GitHub or GitLab, the git push origin command is used with the --delete switch and a reference to the branch to delete.

For example, the following command will delete a remote branch named old-branch:

git@DELETE /c/local/remote/branch (main)
git push origin --delete old-branch
*  [deleted]      alpha
Remote Git branch delete alpha successful

The following image demonstrates how to delete local and remote Git branches:

delete git remote local branch remove

These commands will delete both local and remote Git branches.