gpt4 book ai didi

git - 在 Git 中列出每个分支及其最后修订日期

转载 作者:IT王子 更新时间:2023-10-29 01:20:47 25 4
gpt4 key购买 nike

我需要从我们的远程存储库中删除旧的和未维护的分支。我正在尝试找到一种方法来按上次修改日期列出远程分支,但我做不到。

有没有一种简单的方法可以用这种方式列出远程分支?

最佳答案

commandlinefu有 2 个有趣的命题:

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 --color=always -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提及 in the comments zsh 需要对 sed 表达式进行转义:

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 :

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'

多行:

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'

备注:n8transwer , 基于 git for-each-ref refs/heads更清洁。 And faster .
另见“Name only option for git branch --list?

更一般地说,tripleee提醒我们in the comments :

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

(我在 2014 年用“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

关于git - 在 Git 中列出每个分支及其最后修订日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2514172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com