DEV Community

Mike Street
Mike Street

Posted on • Originally published at mikestreety.co.uk on

How to delete a Git branch

Deleting branches is important to ensure old code doesn't hang around or the wrong thing doesn't get merged

Note: In the examples below pow will be used for the branch name

Delete a local branch

If you have a local branch you want to delete you can run

git branch -d pow
Enter fullscreen mode Exit fullscreen mode

If your branch has not been merged into your current branch you need to change the -d to a capital:

git branch -D pow
Enter fullscreen mode Exit fullscreen mode

Delete a remote branch

If you wish to delete the branch via command line, you have to "push" it with a colon (:) preceding the name

git push origin :pow
Enter fullscreen mode Exit fullscreen mode

Updating your local branch data

If you deleted your remote branch from another computer (or via the website if on Github/Gitlab), you might find it is still listed when running a git branch -a. This means your local Git repository thinks the branch still exits and could cause conflicts if you try to create a branch of the same name.

To remove these, you can fetch with an additional --prune parameter

git fetch origin --prune
Enter fullscreen mode Exit fullscreen mode

Tip: If you want it to always prune when you do a git fetch origin, you can set this as a global setting:

git config --global fetch.prune true
Enter fullscreen mode Exit fullscreen mode

Read time: 1 mins

Tags: Git

Top comments (0)