How can I delete all git branches that were squash and merge through github?

Since GitHub introduced Squash and Merge , all the cool kids in my workplace have been using it when merging download requests. Is there a way to clean squash and merge branches?

Next command How to remove all git branches that have been merged? not working for squash and merge:

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d 
+8
source share
2 answers

There is no easy way to automate this, at least not completely. (Some special cases can be handled.) Instead, it is best to delegate this delete branch to the person whose pull request has been merged. There are several good reasons for this:

  • They are the only ones who can be sure that the merger is done correctly.

    Suppose, for example, that in order to squash-combine a series of six commits, the person who performed the squash merge should or have decided to change several characters somewhere in a line or two, for some reason good or bad. This line or two means that the total change in the final commit is different from the sum of the six changes in the six fixes.

    But is the result correct? If you do not make any changes yourself, how do you know?

  • They are the only ones who know if they intend to continue to develop in this thread.

    Just because the six feature/tall were compressed into a single message added to devel does not mean that feature/tall is done. They can add a few more commits; they may want to reload feature/tall on devel again, dropping six crushed commits in favor of one hex squash, but keeping three more commits that they are going to add.

There are probably a few more cases. All of this can be rare; they can never happen in your project; but the point here is that the feature/tall branch is their branch, not your branch, so they - whoever they are - should be the ones that delete it when it is done.

Note that when you select feature/tall , you have your own Git to rename it to origin/feature/tall (if your remote is called origin ). If you experiment with it and git checkout feature/tall , your Git will make a copy for you. Once they remove feature/tall and you run git fetch origin --prune , your Git will remove your origin/feature/tall . So, now the problem is simpler and can be automated: find branches that do not have "upstream", and delete them . (There are some minor flaws in one line of the script in this answer, see Comments; a more attractive one would be to use git for-each-ref and look at each branch upstream with git rev-parse , but this is probably overkill.)

+2
source

Here is a script that will delete all the local branches that were squash, merged into a master:

 git checkout -q master && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base master $branch) && [[ $(git cherry master $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done 

If you want to run a trial run, you can run instead:

 git checkout -q master && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base master $branch) && [[ $(git cherry master $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && echo "$branch is merged into master and can be deleted"; done 

Then you can configure the alias like this:

 alias gprunesquashmerged='git checkout -q master && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base master $branch) && [[ $(git cherry master $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done' 

A source:

https://github.com/not-an-aardvark/git-delete-squashed

0
source

Source: https://habr.com/ru/post/1266896/


All Articles