我需要从我们的远程存储库中删除旧的和未维护的分支。我正在尝试找到一种方法来按其上次修改日期列出远程分支,但我不能。
有没有一种简单的方法可以这样列出远程分支?
最佳答案
commandlinefu有两个有趣的主张:
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
要么:
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
这是使用Unix语法的本地分支。使用
git branch -r
,您可以类似地显示远程分支: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提到了zsh需要对
sed
表达式进行转义的in the comments: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添加in the comments:
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'
在多行中:
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'
注意:基于n8tr的answer的
git for-each-ref refs/heads
更干净。 And faster。另请参阅“Name only option for
git branch --list
?”通常,tripleee会提醒我们in the comments:
(我在2014年用“What is the difference between
$(command)
and `command`
in shell programming?”说明了这一点)