Skip to content

Instantly share code, notes, and snippets.

@lonsun
Created July 11, 2016 22:46
Show Gist options
  • Save lonsun/23df675f631f38f942be53bf4fe28570 to your computer and use it in GitHub Desktop.
Save lonsun/23df675f631f38f942be53bf4fe28570 to your computer and use it in GitHub Desktop.
Fish shell script to remove local Git branches that have been merged and no longer appear on remote.
function git_clean_branches
set base_branch develop
# work from our base branch
git checkout $base_branch
# remove local tracking branches where the remote branch is gone
git fetch -p
# find all local branches that have been merged into the base branch
# and delete any without a corresponding remote branch
set local
for f in (git branch --merged $base_branch | grep -v "\(master\|$base_branch\|\*\)" | awk '/\s*\w*\s*/ {print $1}')
set local $local $f
end
set remote
for f in (git branch -r | xargs basename)
set remote $remote $f
end
for f in $local
echo $remote | grep --quiet "\s$f\s"
if [ $status -gt 0 ]
git branch -d $f
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment