6

In the Git Bash, repositories list what branch they are on when you are inside the repo directory.

Currently I'm working on a project that compiles a group of repositories into one product and when I'm in the directory that contains all of those repositories, it would be very helpful to be able to see the current branch for each one listed next to it when I ls.

Is there a setting or a way I could make Git Bash behave this way? Perhaps an ls flag I'm unfamiliar with (tho this seems doubtful)?

Stefan Neacsu
  • 653
  • 3
  • 12
burnst14
  • 95
  • 2
  • 11
  • 1
    No, `ls` doesn't know nothing about `git`, it certainly cannot show the current branch. You have to write a script yourself. Loop for every subdirectory in the project directory: `cd` into subdirectory, print the current branch, return to the parent directory. – phd Nov 25 '19 at 16:31

1 Answers1

5

The command you can execute from your current folder is:

git --git-dir=subfolder/.git branch --show-current

(--show-current is available with Git 2.22, Q2 2019)

So you need to loop over your subfolder and use that to get a list of all branches:

As a variant, with -execdir:

find . -maxdepth 2 -type d -name ".git" -exec echo -n "{} :" ; -execdir git branch --show-current ;

Depending on your bash, you might need to escape the ;: \;

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Perfect, thank you very much! This works like a charm! – burnst14 Nov 25 '19 at 18:10
  • Okay I've been working through this for a while and I can't figure out where I would filter the output of the git command. I want to pipe the output for each directory into a filtering command to get rid of some superfluous information. It doesn't like when I put it after the last semicolon, or when I put it before that semicolon, after the git command. – burnst14 Nov 25 '19 at 19:04
  • @burnst14 Can you edit your question with what you have as an output, and the kind of end result you want? – VonC Nov 25 '19 at 19:14
  • @burnst14 generally, using a pipe after that all command should allow you to filter what you need. Ie: `find . -maxdepth 2 -type d -name ".git" -exec echo -n "{} :" ; -execdir git branch --show-current ;|sed "s,/,,g"` would remove any '/' (just an example) – VonC Nov 25 '19 at 22:23
  • I see, thank you! I was confused by the semicolons and what they meant, was taking them for pipes. Thanks so much for your help! – burnst14 Nov 26 '19 at 15:44