Trimming local git branches

Every now and then I remember to clean local, and merged git branches, and I always forget how to do it. So, TIL how to think about it, thanks to this post.

  1. Get on the main branch.

  2. Run a script using the following procedure:

    1. List all of the branch names
    2. Look for the asterisk (*) and ignore it (make sure it’s verbose)
    3. Pass all of the other branches and run the git branch deletion command

Here’s how it looks.

git checkout main && \
   git branch | \
   grep -v '^*' | \
   xargs git branch -d

I’ve created and alias in .zshrc for this command as git-trim. Enjoy, future Dave!