Acidorr
Acidorr

Reputation: 35

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?

Upvotes: 2

Views: 541

Answers (2)

nbari
nbari

Reputation: 26905

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:

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

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.

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 \;'

Upvotes: 0

ElpieKay
ElpieKay

Reputation: 30858

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)}'

I'm not good at awk and this is just a rough solution.

Upvotes: 2

Related Questions