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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Developer Git Commit Hygiene
  • A Comprehensive Guide to GitHub
  • Mastering Git
  • Understanding Git

Trending

  • Accelerating AI Inference With TensorRT
  • Why Documentation Matters More Than You Think
  • Zero Trust for AWS NLBs: Why It Matters and How to Do It
  • Performance Optimization Techniques for Snowflake on AWS
  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 user avatar
Loïc Rouchon
·
Feb. 06, 22 · Code Snippet
Likes (5)
Comment
Save
Tweet
Share
3.7K 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.

Related

  • Developer Git Commit Hygiene
  • A Comprehensive Guide to GitHub
  • Mastering Git
  • Understanding Git

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!