DEV Community 👩‍💻👨‍💻

Erik Smith
Erik Smith

Posted on

Git Bulk Delete Branches Example

In this example we want to delete all branches from our local git repo that begin with fix/.

1. Identify all branches in a github repo that start with fix/.

git branch --list "fix/*"
Enter fullscreen mode Exit fullscreen mode

You will see a list of branches matching the glob pattern, provided there are any.

  fix/debug-console-error
  fix/dropcase-first-letter
  fix/font-size
  fix/off-by-one
Enter fullscreen mode Exit fullscreen mode

2. If this list of branches looks correct, you can delete them all by piping it to xargs git branch -d.

git branch --list "fix/*" | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

You might see some errors like these:

error: The branch 'fix/off-by-one' is not fully merged.
If you are sure you want to delete it, run 'git branch -D fix/off-by-one'.
Enter fullscreen mode Exit fullscreen mode

This lets you know that your "main" (or the currently checked out branch) does not have all commits from the fix/off-by-one branch you are trying to delete. If you delete the branch, you will lose those changes and any history associated with them. If you feel losing that information is fine for your needs, you can run xargs again, this time passing the -D flag to git.

git branch --list "fix/*" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode

3. Once more listing the branches beginning with fix/ should no longer return any results.

git branch --list "fix/*"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

🌚 Life is too short to browse without dark mode