A script to setup git submodules from a configuration file
https://github.com/leog/git-setup-submodules.git
A CLI tool to automate Git submodule setup from a configuration file.
# Create a config file
echo "libs/shared-utils" > .git-setup-submodules
# Run the setup
npx git-setup-submodules
This tool automates the setup of Git submodules in your project based on a configuration file. It reads the configuration, checks access to the specified repositories, adds them as submodules at specified paths, and sets them to specific branches or tags.
--config.git restore --staged)You can use the script as a local dependency in your project or run it directly using npx.
cd /path/to/your/project
npm install git-setup-submodules --save-dev
npxYou can run the script without installing it by using npx:
npx git-setup-submodules
git-setup-submodules [OPTIONS]
OPTIONS:
-h, --help Show help message
-n, --dry-run Preview changes without executing git commands
-q, --quiet Suppress non-essential output
-c, --config <path> Path to config file (default: .git-setup-submodules)
-b, --default-branch <name>
Default branch when not specified (default: main)
# Standard usage
git-setup-submodules
# Preview what would happen
git-setup-submodules --dry-run
# Use a custom config file
git-setup-submodules --config ./submodules.conf
# Use 'master' as the default branch
git-setup-submodules --default-branch master
# Quiet mode (only show errors)
git-setup-submodules --quiet
Create a configuration file named .git-setup-submodules in the root directory of your project. This file lists the submodules to be added along with their configurations.
Each line in the configuration file represents a submodule and follows this format:
<remote-path>[:<local-path>][#<branch-or-tag>]
<remote-path>: Path to the submodule repository relative to your Git remote URL base.<local-path> (optional): Custom local directory name for the submodule. Defaults to the module name if omitted.<branch-or-tag> (optional): Branch or tag to check out. Defaults to main if omitted.libs/utils
utils from libs/utils into libs/utils, checking out the main branch.apps/website:site
website from apps/website into apps/site, checking out the main branch.libs/logger#v1.2.3
logger from libs/logger into libs/logger, checking out the v1.2.3 branch or tag.apps/website:site#production
website from apps/website into apps/site, checking out the production branch.# or // are treated as comments.// are supported.# Submodules Configuration
# Add the utils library
libs/utils
# Add the website app to 'site' directory on 'production' branch
apps/website:site#production
# Add the logger library at tag 'v1.2.3'
libs/logger#v1.2.3
// Add the helpers library to 'helpersLib' directory
libs/helpers:helpersLib
// Add the console app
apps/console
If you've installed the script as a local dependency, you can run it using npx:
npx git-setup-submodules
Alternatively, you can add a script to your package.json:
{
"scripts": {
"setup-submodules": "git-setup-submodules"
}
}
Then run:
npm run setup-submodules
npx DirectlyIf you haven't installed the script locally, you can run it directly with npx:
npx git-setup-submodules
.gitmodules.gitmodules exists, the script assumes submodules are already initialized and exits..git-setup-submodules for submodule definitions.git ls-remote to check if you have access to the repository.git submodule add --force to add the submodule.git pull.git restore --staged to unstage the submodule and .gitmodules.Make sure the .git-setup-submodules file exists in your project root, or specify a custom path with --config:
git-setup-submodules --config path/to/config
This error occurs when:
git init first.git remote add origin <url>.Check that:
ssh -T git@github.com)The script won't run if submodules are already configured. If you want to reconfigure:
# Remove existing submodules first
rm .gitmodules
rm -rf .git/modules/*
git config --remove-section submodule.<path> # for each submodule
The git restore command requires Git 2.23.0 or later. Update Git:
# macOS
brew upgrade git
# Ubuntu/Debian
sudo apt-get update && sudo apt-get install git
# Check version
git --version
Module paths cannot contain shell metacharacters like ;, |, $, etc. Use only alphanumeric characters, /, -, _, and ..
Use --dry-run to preview what commands would be executed without making changes:
git-setup-submodules --dry-run
.git-setup-submodules is not found.git checkout -b feature/your-feature-name
npm test
git commit -m "Add your feature"
git push origin feature/your-feature-name
# Install dependencies
npm install
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage
This project uses commit-and-tag-version for versioning and changelog generation. Releases follow Conventional Commits.
<type>(<scope>): <description>
[optional body]
[optional footer(s)]
Types:
feat: A new feature (bumps minor version)fix: A bug fix (bumps patch version)docs: Documentation only changesstyle: Code style changes (formatting, semicolons, etc.)refactor: Code refactoringperf: Performance improvementstest: Adding or updating testschore: Maintenance tasksBREAKING CHANGE: in the footer or ! after the type (e.g., feat!:) to trigger a major version bump.
# Preview what would happen (dry run)
npm run release:dry
# Create a release (auto-determines version from commits)
npm run release
# Create a specific version bump
npm run release:patch # 1.0.0 -> 1.0.1
npm run release:minor # 1.0.0 -> 1.1.0
npm run release:major # 1.0.0 -> 2.0.0
# First release (if starting fresh)
npm run release:first
After running a release command:
# Push the commit and tag
git push --follow-tags origin main
# The GitHub Action will automatically publish to npm when a tag is pushed
This project is licensed under the Apache 2.0.