DZone
Open Source Zone
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
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Open Source Zone > 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.

Loïc Rouchon user avatar by
Loïc Rouchon
·
Feb. 06, 22 · Open Source Zone · Code Snippet
Like (5)
Save
Tweet
2.88K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Popular on DZone

  • To Shift Right, You Need Observability
  • Spring, IoC Containers, and Static Code: Design Principles
  • Are Foreign Keys Unscalable?
  • MEAN vs MERN Stack: Which One Is Better?

Comments

Open Source Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo