How to delete local and remote Git branches

Delete a local and remote Git branch

To delete both a local and remote Git branch, even if the Git branch has the same name locally and remotely, two commands must be issued:

  1. A git push origin delete command deletes the remote Git branch
  2. A git branch delete command deletes the local Git branch

When these two commands are issued, three important Git branch related resources are deleted:

  • The remote Git branch is deleted
  • The local Git branch is deleted
  • The Git repo’s remote tracking branch is deleted

Delete a remote Git branch

It’s not a git branch command that deletes a remote Git branch.

Instead, this happens via the git push command, along with a delete switch and the name of the remote branch to delete.

Remove a remote Git branch example

For example, to delete a remote Git branch named alpha, you would issue the following command:

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

After you issue this command, inspect the remote repository and you would see the removal of the remote Git branch in question.

This command also removes all remote tracking branches that point to the associated branch in the remote repo.

delete git remote local branch remove

These commands delete both local and remote Git branches.

Delete a local Git branch

The deletion of a local Git branch is relatively straightforward.

To delete a local Git branch, simply use the git branch command with the –delete switch and reference the name of the local Git branch to remove.

Remove a local Git branch example

For example, to delete a local Git branch named alpha, the command is as follows:

git@DELETE /c/local/remote/branch (main)
git branch --delete alpha
Deleted local Git branch alpha (was 4d2cd72)

Confirm Git branch deletion

To confirm the deletion of the local branch, simply issue a git branch -a command. This lists all local Git branches and all remote Git tracking branches.

git@DELETE /c/local/remote/branch (main)
git branch -a
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main

No reference to the deleted local branch, remote tracking branch or the deleted remote Git branch should remain.