Deleting branches in Git

For some projects, the workflow I use in Git is to create a new branch for every new feature or bug fix. This works well for keeping things separate, but it does result in a lot of branches lying around which are no longer needed after they’ve been merged into master. There are a few steps which will clean up old branches one at a time.

First of all, to remove the branch from the remote, push as you normally would but place a colon in front of the branch name:

git push origin :mybranch

Then delete your local copy of the branch:

git branch --delete mybranch

Anyone else with a copy of the repository can then run the following command to prune their list of remote branches:

git fetch origin --prune

You can use -d instead of --delete and -p in place of --prune.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.