繁体   English   中英

列出目录时显示git分支-Ubuntu Terminal

[英]Showing git branch when listing directories - Ubuntu Terminal

只是一个简单的问题,列出这些文件夹时,有什么方法可以列出不同文件夹的分支吗?

我的意思是,说您有几个具有不同项目的文件夹,并且想要执行ls -l ,还可以查看不同文件夹中的哪个分支,有没有办法?

ls -l  | awk '{cmd="git --git-dir="$NF"/.git branch 2>/dev/null| grep ^*";system(cmd "> .tmp");getline branch < ".tmp";close(".tmp");print $0" "branch} END {cmd="rm -rf .tmp";system(cmd)}'

我不擅长awk ,这只是一个粗略的解决方案。

这将打印目录和当前目录:

find . -type d -depth 1 -print0 -exec git --git-dir={}/.git rev-parse --abbrev-ref HEAD \;

输出将类似于:

./dir<branch>

要使用空格./dir<space><branch>改善输出,可以这样做:

find . -type d -depth 1 -print0 -exec sh -c 'b=$(git --git-dir={}/.git rev-parse --abbrev-ref HEAD); echo " $b"' \;

要查看所有分支,这将起作用:

find . -type d -depth 1 -print0 -exec git --git-dir={}/.git branch \;

注意rev-parse --abbrev-ref HEAD vs branch

它假定您在一个目录中有多个存储库,也可以通过替换branch来进行pull ,以使所有项目保持同步。

别名可能会变得很方便,例如:

alias pb='find . -type d -depth 1 -print0 -exec git --git-dir={}/.git rev-parse --abbrev-ref HEAD \;'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM