Skip to content

Instantly share code, notes, and snippets.

@leeopop
Created March 23, 2021 01:42
Show Gist options
  • Save leeopop/c7341beb8e5bb94cdd543e770977ead6 to your computer and use it in GitHub Desktop.
Save leeopop/c7341beb8e5bb94cdd543e770977ead6 to your computer and use it in GitHub Desktop.
Remove untracked branches on git
#!/bin/bash
set -o noglob
git remote update --prune
raw_input=$(git branch -vv)
while read -r line; do
branch_line=$(echo "$line" | sed s/*//g)
branch_line=$(echo $branch_line)
branch_name=${branch_line%% *}
if [[ $branch_line =~ "[origin/" ]]
then
has_origin=true
else
has_origin=false
fi
if [[ $branch_line =~ ": gone" ]]
then
has_gone=true
else
has_gone=false
fi
if [ $has_gone = true ] || [ $has_origin = false ]
then
echo "Removing branch $branch_name ..."
git branch -d $branch_name
fi
done <<< "$raw_input"
#!/bin/bash
set -o noglob
git remote update --prune
raw_input=$(git branch -vv)
while read -r line; do
branch_line=$(echo "$line" | sed s/*//g)
branch_line=$(echo $branch_line)
branch_name=${branch_line%% *}
if [[ $branch_line =~ "[origin/" ]]
then
has_origin=true
else
has_origin=false
fi
if [[ $branch_line =~ ": gone" ]]
then
has_gone=true
else
has_gone=false
fi
if [ $has_gone = true ] || [ $has_origin = false ]
then
echo "Removing branch $branch_name ..."
git branch -D $branch_name
fi
done <<< "$raw_input"
git remote update --prune; git branch -vv | % {
$branch_line = $_.toString().Replace("*","").Trim();
$branch_name = $branch_line.Replace("*","").Trim().Split(" ")[0];
$has_origin = $branch_line.Contains("[origin/");
$has_gone = $branch_line.Contains(": gone");
if ($has_gone -or -not $has_origin) {
git branch -D $branch_name;
}
}
git remote update --prune; git branch -vv | % {
$branch_line = $_.toString().Replace("*","").Trim();
$branch_name = $branch_line.Replace("*","").Trim().Split(" ")[0];
$has_origin = $branch_line.Contains("[origin/");
$has_gone = $branch_line.Contains(": gone");
if ($has_gone -or -not $has_origin) {
git branch -d $branch_name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment