简体   繁体   中英

Git : Find all remote branches eligible* for deletion (* last commit is a merge to master )

I know this question has been broached already in a similar vein . However I'm looking for a stricter eligibility criterion viz . I'm interested only in branches which

  1. have been merged to master
  2. have no further commits after the merge to master

This is to avoid deleting branches which have some further ( possibly valuable ) work but which have not been merged up to master ( through possible omission , abandoned pull request etc. )

Here is the proposed solution:

Assumption : The oldest ancestor is assumed to be origin/master . This is standard practice but no harm to call it out .

git branch -r --list --contains `git rev-list  --max-parents=0 origin/master`| grep -v origin\/master | sed 's/^..\(.*\)/\1/' | xargs -I {}  sh -c "echo -n {}' : '; git log --graph -n 1 {} | grep -c '^|[\]\ \ Merge'"

Step by Step

  1. Find all remote branches which have the initial master commit as a descendant .
  2. For each branch check that the latest commit is a merge back to oldest ancestor.

The output report looks like the following . A 1 alongside the branch means it is eligible to delete

origin/0.2016.02-release : 1
origin/0.2016.03-release : 1
origin/Fix-for-NPE-schedule-master : 0
origin/Fix-possible-infinite-loop-master : 0
origin/Fixing-unit-tests-master : 0
origin/NoBodyRequiredForActivity-master : 0
origin/Removing-not-needed-dependency-master : 0
origin/updateDevServiceConfig-master : 0
origin/updateServerPort-master : 0
origin/updatedPom : 1

Hope this helps anyone looking for something similar . Would welcome any suggestions and improvements

Much simpler, for this particular case (checking what remote-tracking branches are fully merged into master ):

git branch -r --merged master

(and of course --no-merged for ones that may still need work on them, as per comments on the other answer ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM