in our team we keep the fast-forward only merge policy for master and development branches in order to prevent merge commit hell:
I do not delete my topic branches once they are merged (or rebased and then merged), so I end up with tons of these. I can delete some:
git branch --merged
This will only show me those which hasn't been rebased prior merge. And there are some of these, I am able to clean them up.
I am looking for some strategy, script or hint how to deal with the rebased ones. There must be a script that finds all the commits from the topic branch in the master in a loop or something. Please share ;-)
Thanks
I wanted something non-interactive that generalizes to more than one commit per branch. With the caveat that this hasn't been tested extensively yet, and may be assuming more than you do about the state of your clone when you run this:
Rebase all local branches that can be rebased cleanly:
REBASE_TARGET=upstream/master
for branch in $(git for-each-ref --format="%(refname:lstrip=2)" refs/heads/); do
git rebase "$REBASE_TARGET" "$branch" || git rebase --abort
done
Remove any local branches that have been merged:
git branch --format="%(refname:lstrip=2)" --merged | xargs git branch -d
You have one script which does that by:
That would delete rebased branches which have been merged to master.
last_commit_msg="$(git log --oneline --format=%f -1 $branch)"
if [[ "$(git log --oneline --format=%f | grep $last_commit_msg | wc -l)" -eq 1 ]]; then
I wrote an interactive script that can help with this.
https://github.com/lzap/git-xcleaner
Send patches and read the warning from it's manual page (bellow)!
git-xcleaner(1) -- interactive git branch removal SYNOPSIS
git xcleaner
DESCRIPTION
This tool helps with deleting unused topic branches using TUI (text user interface). It also offers mechanisms for pre-selecting branches that can be safely removed.
WARNING
This tool deletes git branches after confirmation. There is no way back when git garbage is collected. Double check what you are about to remove!
RETURN VALUES
Zero on success, one on errors when manipulating with git.
BUGS
File bugs at https://github.com/lzap/git-xcleaner/issues
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With