Tutorials Git & Version Control How to Delete all Local Branches in Git?

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

Now run the shell script

$ ./deleteLocalGitBranches.sh
Total
0
Shares
guest
0 Comments
Inline Feedbacks
View all comments
Previous Post

Proliferation Caused by Java Unified GC Logging

Next Post

Git Diff – Inspecting and Comparing Changes in Git

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
0
Would love your thoughts, please comment.x
()
x