Let’s talk about git repositories! I don’t want to get into the mono versus multi-repository discussion. Some advocates for mono-repositories and others against them.

But no matter which side you’re on, chances are you contribute to repositories parts of different organizations: personal projects, professionals, or various open-source ones. In this case, it is for sure not possible to use a mono-repository approach.

Hence, I thought of a solution to help me keep some repositories up-to-date with their remotes or ensure I did push everything. No need for fancy things, a small shell script can do the work.

Please welcome gitr! gitr is an alias in my fish.config/.bashrc for a simple shell script I wrote.

You said gitr, what’s that?

Gitr stands for git recurse. It expects a git command and will execute it in every subdirectory that is a git repository. It is very basic and only tailored to my needs. So don’t expect it to handle symbolic links or git submodules.

Usage

Let’s assume you are in a directory and have the following git repositories under it:

  • oss/jreleaser

  • oss/picocli

  • personal/xxx.github.io

  • personal/website

Let’s now assume you want to fetch the different remotes to prepare for offline work. git has a command for that, git fetch --all. with gitr, all you need to do is replace git with gitr.

Hence: gitr fetch --all

$ gitr fetch --all

## oss/jreleaser
Fetching origin

## oss/picocli
Fetching origin
Fetching upstream

## personal/xxx.github.io
Fetching origin

## personal/website
Fetching origin

Want to check if you committed and pushed everything? gitr status got you covered!

$ gitr status

...

## personal/xxx.github.io
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

## personal/website
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    content/posts/dealing-multiple-git-repositories.md

nothing added to commit but untracked files present (use "git add" to track)

Obviously, I need to finish the content/posts/dealing-multiple-git-repositories.md article and publish it.

Summary

gitr is a very small script that can help manage multiple git repositories by calling git itself in every repository. You get all the power of git fetch, pull, status, …​

One last thing, be careful with it and think twice before calling gitr reset --hard origin/main or gitr commit -m "xxx".

May it be helpful to you!

gitr.sh can be downloaded here or you can see the source below.
#!/bin/sh
set -eu

recurse() {
    (
        cd "$1"
        shift
        if [ -d ".git" ]; then
            # If current directory is a git directory,
            # call the git command
            printf "\033[1;34m## $(pwd)\033[0m\n"
            git "$@"
            printf "\n"
        else
            # Otherwise, recurse in sub-directories
            for d in ./*; do
                if [ -d "$d" ]; then
                    recurse "$d" "$@"
                fi
            done
        fi
    )
}

recurse "." "$@"