Skip to content

Instantly share code, notes, and snippets.

@tabuchid
Created May 14, 2012 16:14
Show Gist options
  • Select an option

  • Save tabuchid/2694841 to your computer and use it in GitHub Desktop.

Select an option

Save tabuchid/2694841 to your computer and use it in GitHub Desktop.

Revisions

  1. tabuchid created this gist May 14, 2012.
    31 changes: 31 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    function gitclean {
    current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
    if [ "$current_branch" != "master" ]; then
    echo "WARNING: You are on branch $current_branch, NOT master."
    fi
    echo "Fetching merged branches..."
    git remote prune origin
    remote_branches=$(git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$")
    local_branches=$(git branch --merged | grep -v 'master$' | grep -v "$current_branch$")
    if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then
    echo "No existing branches have been merged into $current_branch."
    else
    echo "This will remove the following branches:"
    if [ -n "$remote_branches" ]; then
    echo "$remote_branches"
    fi
    if [ -n "$local_branches" ]; then
    echo "$local_branches"
    fi
    read -p "Continue? (y/n): " -n 1 choice
    echo
    if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
    # Remove remote branches
    git push origin `git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$" | sed 's/origin\//:/g' | tr -d '\n'`
    # Remove local branches
    git branch -d `git branch --merged | grep -v 'master$' | grep -v "$current_branch$" | sed 's/origin\///g' | tr -d '\n'`
    else
    echo "No branches removed."
    fi
    fi
    }