DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Shrink a Bloated Git Repository and Optimize Pack Files
  • Developer Git Commit Hygiene
  • A Comprehensive Guide to GitHub
  • Mastering Git

Trending

  • Member Spotlight: Raghava Dittakavi
  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • You Do Not Need a PhD to Become an AI Engineer: The Essential Skill Stack

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Dealing With Multiple Git Repositories

Dealing With Multiple Git Repositories

Today, I'll share my repositories maintenance solution - gitr, a small script to recursively call Git in sub-folders to let you manage multiple repositories with ease.

By 
Loïc Rouchon
·
Feb. 06, 22 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
4.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Let’s talk about git repositories! I don’t want to get into the mono versus multi-repository discussion. Some advocate for mono-repositories and others are 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!

The script can be downloaded here or you can see the source below:

Shell
 
#!/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 "." "$@"


Git Repository (version control)

Published at DZone with permission of Loïc Rouchon. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Shrink a Bloated Git Repository and Optimize Pack Files
  • Developer Git Commit Hygiene
  • A Comprehensive Guide to GitHub
  • Mastering Git

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook