Post

Delete local git branches starting with specific text

Delete local git branches

You can delete individual local git branch by using git command or any git tool easily

Git command to delete individual branch

1
2
# command
git branch -d feature/awsome-feature

Use -D instead of -d if you want to force the delete.

This works fine for single deletes but if you have lot of local branches, it can be cubersome to delete individual branches

Delete multiple branches at once

Use fetch with prune

1
2
3
4
# Fetch changes from all remotes and locally delete 
# remote deleted branches/tags etc
# --prune should delete the tracking branches/tags which are deleted on remote server
git fetch --all --prune

Use grep to scan local branches with specific text and delete

1
2
# grep command will get the branches which starts with text 'feat'
git branch -D `git branch | grep -E '\bfeat'`

In the above command \bfeat refers to start of the string from git branch output

This post is licensed under CC BY 4.0 by the author.