Skip to content

Instantly share code, notes, and snippets.

@dflima
Last active July 8, 2017 02:50
Show Gist options
  • Save dflima/c5137785c437fcdf0c74a544bfc290fc to your computer and use it in GitHub Desktop.
Save dflima/c5137785c437fcdf0c74a544bfc290fc to your computer and use it in GitHub Desktop.
Deletes branches merged with <branch>
#!/bin/bash
function usage()
{
printf "Usage:\n\n"
printf "./delete_merged_branches <branch>\n\n"
}
if [ "$1" == "" ]; then
usage
exit 0
fi
in_branch="$(git branch | grep \* | cut -d ' ' -f2)"
out_branch=$1
git checkout $out_branch;
git pull;
git branch --merged | grep -v $out_branch | xargs git branch -D;
git checkout $in_branch;
@caiotava
Copy link

caiotava commented Sep 8, 2016

@dflima, add a git pull

git checkout $out_branch;
git pull;
git branch --merged | grep -v $out_branch | xargs git branch -D;

@webysther
Copy link

@webysther
Copy link

For clean projects using /feature or /fix.

$ git fetch --prune;
$ git branch --remote --merged |
    grep origin |
    grep -v '>' |
    grep -v master |
    grep -v develop |
    xargs -L1 |
    cut -d"/" -f2- |
    xargs git push origin --delete;

@dflima
Copy link
Author

dflima commented Nov 4, 2016

@caiotava nice approach, thanks!

@dflima
Copy link
Author

dflima commented Nov 4, 2016

@webysther that's a nice feature, but I don't want to delete remote branches. Maybe I can include that as an option. :)

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