How to delete a git branch locally and remotely

February 10, 2021
views 1 min read

I've been working on changing all my repos default branch from master to main. Guess I'm still missing a few, but I've changed most of it.

Since I've been using this so much I decided to write a quick article on how to delete branches both locally and remotely.


Locally

To delete a branch locally you can simply run:

git branch -d <name-of-the-branch>

Example:

git branch -d feature/align-button

In case your branch haven't been pushed to the remote origin -d will not work. In case you want to force delete a branch use -D.


Remotely

To delete a branch remotely is a little different:

git push <remote> --delete <branch>

Example:

git push origin --delete feature/align-button

Bonus

When you're fetching data from a remote repository use the -p flag. This will remove all branches locally that don't exist remotely.

Code:

git fetch -p
Read next
Performance Matters
January 10, 2020
views