I've found a great solution for pulling from the current branch from all sub-directories that have a .git folder, even if each repo has a different branch checked out.
The command is a one-liner, flexible enough to modify for different git commands, and can be aliased.
Just copy and paste the following:
find . -type d -maxdepth 2 -name .git -exec sh -c 'cd "$0" && cd .. && git pull origin $(git rev-parse --abbrev-ref HEAD)' {} \;
Breaking it down:
find . -type d -maxdepth 2 -name .git
Find all directories (-type d) in the current directory (find .) that have the name ".git" (-name .git), looking a maximum of two directories deep (2 rather than 1 because we're looking for the git folder within the git repo folder).
-exec sh -c
Run the following shell command (exec sh -c)
'cd "$0" && cd .. && git pull origin $(git rev-parse --abbrev-ref HEAD)'
Change directory to the first argument (cd "$0"), then change directory one level up to leave the .git folder (cd ..) then do git pull origin while specifying the branch by running git rev-parse... for the current branch's HEAD commit.
{} \;
The "{}" is the result relative path we get from the initial find command. The ; is used to end the command.
Tested on MacOS 10.14.6 on zsh. As-is, works only when remote and local branches are named the same, AFAIK.
You can modify this to get status. I expect you might be able to add arguments to further make this more flexible but I haven't tried yet.
hg mercurial
. – Tallougit for-each-repo
command – Scarrowgit for-each-repo
is that prints the results of the commands, but doesn't say which results corresponds to which repo – Selfcommandgit rev-parse --git-dir
for your command sequence in agit for-each-repo
loop: that would display each repository root folder. – Scarrowgit for-each-repo
loop? – SelfcommandYou can run git for-each-repo --config=<config> -- xxx args
: that will look for agit-xxx
executable in$PATH
, in which you can put as many comma d as you want. – Scarrow