DEV Community

Nilanchal
Nilanchal

Posted on • Originally published at stacktips.com on

1 1

How to Delete all Local Branches in Git?

When working with a larger team and with a proper Git flow process, the number of local feature branches are grows in your local machine. Not that they do any harm to your project, but they can get quite confusing at times. This little code snippet will be able to delete all other local branches except master, develop or release/*.

Create a file named, deleteLocalGitBranches.sh and add the following code snippet.

#!/bin/bash
# Move to master branch. Delete all other local branches except master, develop, release/* or project/*

# Move to master branch
git checkout master

# Collect branches
branches=()
eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/heads/)"

for branch in "${branches[@]}"; do
  old="refs/heads/"
  branchName=${branch/$old/}
  if [["$branchName" != "master" && "$branchName" != "develop" && "$branchName" != "release/"*]]; then
    git branch -D $branchName
  fi
done

Enter fullscreen mode Exit fullscreen mode

Now run the shell script

$ ./deleteLocalGitBranches.sh

Enter fullscreen mode Exit fullscreen mode

The post How to Delete all Local Branches in Git? first appeared on Stacktips.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more