Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@leshill
Last active April 15, 2024 13:13
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leshill/9a1088a17f94cef24831 to your computer and use it in GitHub Desktop.
Save leshill/9a1088a17f94cef24831 to your computer and use it in GitHub Desktop.
Prune local tracking branches that have been removed upstream. Place file in your path (perhaps `~/bin` or `~/scripts`).
#!/bin/bash
#
# Prune local tracking branches that have been removed upstream.
# Your remote tracking branches can be removed automatically by setting `fetch.prune` to true or running `git fetch -prune`.
# Another command to clean up your remote tracking branches is `git remote prune <remote>`.
#
# Author: @leshill
# https://gist.github.com/leshill/9a1088a17f94cef24831
if [[ $# = 1 && $1 == '-n' ]]; then
git branch -vv | grep ' gone]'
elif [[ $# = 1 && $1 == 'all' ]]; then
git branch -vv | grep ' gone]' | egrep -v '^\*' | cut -f 3 -d ' ' | xargs git branch -D
else
echo "usage: git prunelocal [-n | all]"
echo " -n : dry run, show what will be pruned and the current branch if remote gone"
echo " all : remove local tracking branches where the remote is gone, does not remove current branch"
fi
@yas375
Copy link

yas375 commented Aug 4, 2014

Nice) Thank you!

@bruno
Copy link

bruno commented Aug 6, 2014

Neat! Thank you! 👍

@kylejohnsonkj
Copy link

Works perfectly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment