måndag 28 augusti 2017

Time to clean up the repo? Commands to list and sort git branches

As a preparation for a future branch cleanup in our git repos at work I googled for and tried a few branch listing and sorting commands that might come in handy.

List branches 
sorted by last commit date
 git for-each-ref --sort=committerdate refs/remotes/ --format='%(committerdate:short) %(refname:short)'  

Example result
 2016-09-05 origin/PBI-0096  
 2016-10-02 origin/PBI-146   
 2016-11-25 origin/PBI-1320  

List branches 
with age and last committer name 
sorted by last commit date
 for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%ai %ar by %an" $branch | head -n 1` \\t$branch; done | sort -r  

Example result
 2016-10-06 16:17:17 +0200 11 months ago by Harry Potter     origin/PBI-88  
 2016-10-05 14:24:24 +0200 11 months ago by Joe Labero    origin/PBI-146  
 2016-09-02 12:10:17 +0200 12 months ago by Nicola Tesla    origin/PBI-0096  

List branches 
with age and last committer name 
sorted by last committer
 for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --pretty=format:"%Cred %cn %>|(40) %Cblue %ar %>|(80) %Creset" $branch | head -n 1` $branch; done | sort -r  

Example result
 Harry Potter 3 days ago origin/PBI-5124  
 Harry Potter 8 weeks ago origin/PBI-4584  
 Harry Potter 2 days ago origin/PBI-5129  
 Joe Labero 7 weeks ago origin/PBI-4590  
 Joe Labero 6 weeks ago origin/PBI-4742  

Inga kommentarer:

Skicka en kommentar