Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

maintenance - Listing each branch and its last revision's date in Git

I need to delete old and unmaintained branches from our remote repository. I'm trying to find a way with which to list the remote branches by their last modified date, and I can't.

Is there an easy way to list remote branches this way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 1\t$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'

Note: n8tr's answer, based on git for-each-ref refs/heads is cleaner. And faster.
See also "Name only option for git branch --list?"

More generally, tripleee reminds us in the comments:

  • Prefer modern $(command substitution) syntax over obsolescent backtick syntax.

(I illustrated that point in 2014 with "What is the difference between $(command) and `command` in shell programming?")

  • Don't read lines with for.
  • Probably switch to git for-each-ref refs/remote to get remote branch names in machine-readable format

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...