Listing each branch and its last revision's date in git
I need to delete old and unmaintained branches from our remote repo. I'm trying to find a way with which to list the remote branches by their last modified date, and I can't.
Does someone know of an easy way to list remote branches this way?
commandlinefu has 2 interesting propositions:
for k in `git branch | perl -pe s/^..//`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1`t$k; done | sort -r
or:
for k in `git branch | sed s/^..//`; do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k --`t"$k";done | sort
That is for local branches, in a Unix syntax. Using git branch -r
, you can similarly show remote branches:
for k in `git branch -r | perl -pe 's/^..(.*?)( ->.*)?$/1/'`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1`t$k; done | sort -r
Michael Forrest mentions in the comments that zsh requires escapes for the sed
expression:
for k in git branch | perl -pe s/^..//; do echo -e git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1t$k; done | sort -r
kontinuity adds in the comments:
If you want to add it your zshrc the following escape is needed.
alias gbage='for k in `git branch -r | perl -pe '''s/^..(.*?)( ->.*)?$/1/'''`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1`t$k; done | sort -r'
In multiple lines:
alias gbage='for k in `git branch -r |
perl -pe '''s/^..(.*?)( ->.*)?$/1/'''`;
do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- |
head -n 1`t$k; done | sort -r'
Here is what I use:
git for-each-ref --sort='-committerdate:iso8601' --format=' %(committerdate:iso8601)%09%(refname)' refs/heads
This is the output:
2014-01-22 11:43:18 +0100 refs/heads/master
2014-01-22 11:43:18 +0100 refs/heads/a
2014-01-17 12:34:01 +0100 refs/heads/b
2014-01-14 15:58:33 +0100 refs/heads/maint
2013-12-11 14:20:06 +0100 refs/heads/d/e
2013-12-09 12:48:04 +0100 refs/heads/f
For remote branches, just use "refs/remotes" instead of "refs/heads":
git for-each-ref --sort='-committerdate:iso8601' --format=' %(committerdate:iso8601)%09%(refname)' refs/remotes
You may want to call "git fetch --prune" before to have the latest information.
Just to add to the comment by @VonC, take your preferred solution and add it to your ~/.gitconfig alias list for convenience:
[alias]
branchdate = !git for-each-ref --sort='-authordate' --format='%(refname)%09%(authordate)' refs/heads | sed -e 's-refs/heads/--'
Then a simple "git branchdate" prints the list for you...
链接地址: http://www.djcxy.com/p/19492.html上一篇: 如何使用bash脚本遍历所有git分支
下一篇: 在git中列出每个分支及其最新版本的日期