Get git branch from several folders/repos

I have a folder with several repositories inside. Is there any way I can run git branch or whatever git command inside each folder?

$ ls
project1            project2                project3            project4

And I’d like to have some kind of output like the following

$ command
project1 [master]
project2 [dev]
project3 [master]
project4 [master]
Asked By: Luigi T.

||

Try this. $1 should be the parent dir containing all of your repositories (or use “.” for the current dir):

#!/bin/bash

function git_branches()
{
    if [[ -z "$1" ]]; then
        echo "Usage: $FUNCNAME <dir>" >&2
        return 1
    fi

    if [[ ! -d "$1" ]]; then
        echo "Invalid dir specified: '${1}'"
        return 1
    fi

    # Subshell so we don't end up in a different dir than where we started.
    (
        cd "$1"
        for sub in *; do
            [[ -d "${sub}/.git" ]] || continue
            echo "$sub [$(cd "$sub"; git  branch | grep '^*' | cut -d' ' -f2)]"
        done
    )
}

You can make this its own script (but replace $FUNCNAME with $0), or keep it inside a function and use it in your scripts.

Answered By: Will
for dir in */
  do (cd $dir && echo "$dir [$(git rev-parse --abbrev-ref HEAD)]")
done

or even as an alias

alias brall='for dir in $(ls -d */);do (cd $dir && echo "$dir [$(git rev-parse --abbrev-ref HEAD)]") ; done'

It will output (almost) what you want.

project1/ [master]
project2/ [dev]
project3/ [master]
project4/ [master]
Answered By: Antoine
ls | xargs -P10 -I{} git -C {} branch

You can use any git command in place of branch. for e.g:

ls | xargs -P10 -I{} git -C {} pull --rebase
ls | xargs -P10 -I{} git -C {} fetch

Only caveat is output may not contain the directory name. But if it is not an issue, this command is simple.

Answered By: D V Santhosh Kiran

I had to do some tweaks as the git directories were in some sub- sub-folders. It also supports from the cwd or dir specified on $1. Here is an alias to add to .bashrc:

git_branches() {
    if [[ -z "$1" ]]; then
        search_dir=$(pwd)
        echo "Using current dir: '${search_dir}'"
    else
        search_dir=$1
    fi
    
    if [[ ! -d "$search_dir" ]]; then
        echo "Invalid dir specified: '${search_dir}'"
        return 1
    fi
    
    # Subshell so we don't end up in a different dir than where we started.
    (
        cd "$search_dir"
        for dir in $(find . -name .git -type d -prune); do
        (
            cd $dir
            echo "$dir [$(git rev-parse --abbrev-ref HEAD)]"
        );
        done
    )
}
Answered By: Slate

The basic command is

git branch --show-current

in the appropriate folder

so, one liner can be:

for d in project*; do cd "$d" && b="`git branch --show-current`" && cd - >/dev/null && echo "$d [$b]"; done

This can be made an alias or put into a script to be used as a command

Answered By: Oguz Ozhan
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.