Gets rid of your local and remote (origin) branches that have "timob-" in their name and that have already been merged into master (or into whatever other branch you want to specify.)
Not to be confused with the actual git prune
command.
$ git branch --merged master | gprune.py
gprune.py has to be executable, of course. Else python gprune.py
.
To see what it would do, without being destructive, add --test
argument.
The git command ...
$ git branch --merged master
... by itself will show all of your branches which have been merged into master. (If you use a different branch name for your "master" -- like I use "appc_master" -- just substitute it in that git command.)
You can pipe that list of branches into this python script. The script then goes through that list and does some safety checks before deciding to delete a branch or not. The flow/logic:
git fetch origin
is first called to be sure your remote origin branches are updated because we compare them with the local branch (see below).- Branch name must contain "timob-" to be considered for deletion. You might want to change that -- I put it in there as extra safety because I'm most interested in only deleting branches that look like pull request branches. Technically, however, it should be safe to delete any branch that has been merged and that passes the rest of the tests below.
- If you have no
remotes/origin
branch of the same name, your local branch is deleted usinggit branch -d [branch name]
. I don't use-D
. The idea here is that it's safe to delete the branch because a) it's already been merged into master; and b) you don't have a same-named branch up on your origin that could differ from it. If it's your local branch of someone else's branch (i.e., "marshall-timob-4545") and it's anyway merged in master, then we don't really care if it happens to differ from the owner's version of the branch, so it's still safe to delete. - If you do have a
remotes/origin
branch of the same name,git diff --shortstat
is run to compare the local and origin branches. If they differ, neither will be deleted -- i.e., nothing will be done -- because it feels like a work-in-progress. If they don't differ, the local will be deleted withgit branch -d [branch name]
and the remote will be deleted withgit push origin :[branch name]
. The idea here is that if there is no difference between the remote and the local, then they have both been merged into master (since we know the local has already been merged.) Therefore it's safe to delete them both.