简体   繁体   English

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

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

just a quick question, is there any way to list the branches of different folders when listing those folders? 只是一个简单的问题,列出这些文件夹时,有什么方法可以列出不同文件夹的分支吗?

What i mean is, say you have a couple of folders with different projects and you want to do ls -l and also see which branch is in the different folders, is there a way? 我的意思是,说您有几个具有不同项目的文件夹,并且想要执行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 ,这只是一个粗略的解决方案。

This will print the directory and the current: 这将打印目录和当前目录:

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

The output will be something like: 输出将类似于:

./dir<branch>

To improve the output with a space ./dir<space><branch> this could work: 要使用空格./dir<space><branch>改善输出,可以这样做:

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

To see all the branches this will work: 要查看所有分支,这将起作用:

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

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

It assumes you have multiple repositories within a directory, also you could also do a pull by replacing branch to keep all projects in sync. 它假定您在一个目录中有多个存储库,也可以通过替换branch来进行pull ,以使所有项目保持同步。

An alias could become handy, something like: 别名可能会变得很方便,例如:

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