This guide explains how to use the my-utils package across multiple research projects as a Git submodule, ensuring each project remains self-contained and reproducible.
# Add to a new project
git submodule add https://github.com/sangmk/myutils.git myutils
# Clone a project with submodules
git clone --recursive https://github.com/yourusername/your-project.git
# Update utils to latest version
cd myutils && git pull && cd .. && git add myutils && git commit -m "Update myutils"Method 1: Clone with submodules in one command
git clone --recursive https://github.com/yourusername/your-project.gitMethod 2: Clone, then initialize submodules
git clone https://github.com/yourusername/your-project.git
cd your-project
git submodule update --init --recursive# See current submodule commit
git submodule status
# Example output:
# 4a2b3c1 utils (v0.1.0)
# ↑ commit hash that your project is locked toWhen you want to pull recent changes from myutils:
cd myutils
git pull
cd ..
# Commit the updated reference
git add myutils
git commit -m "Update myutils to latest version"To ensure reproducibility, lock to a tagged version:
cd myutils
git fetch --tags
git checkout v0.2.0 # or any specific tag
cd ..
# Commit the version lock
git add myutils
git commit -m "Lock myutils to v0.2.0"cd myutils
git describe --tags # Shows: v0.2.0 or v0.2.0-5-g4a2b3c1When you need to fix or add functions to myutils:
# Work directly in one project's utils submodule
cd /home/workspace/project-A/myutils
# Commit and push to my-utils repo
git add .
git commit -m "Add new function for reading XYZ format"
git push origin main
# Tag new version
git tag -a v0.3.0 -m "Added XYZ reader"
git push origin v0.3.0
# Update parent project reference
cd ..
git add myutils
git commit -m "Update myutils to v0.3.0"cd /home/workspace
mkdir new-analysis
cd new-analysis
git init
git submodule add https://github.com/sangmk/myutils.git utils
# Lock to stable version
cd utils && git checkout v0.2.0 && cd ..
git add .gitmodules utils
git commit -m "Initial commit with utils v0.2.0"
# Create analysis script
cat > analysis.py << 'EOF'
from utils.myutils.io import read_from_log
# ... your code
EOFWhen someone else clones your project:
- They get the exact version of utils you used
- Your results are reproducible
- No manual setup needed
# Collaborator clones your project
git clone --recursive https://github.com/yourusername/your-project.git
cd your-project
python analysis.py # Works immediately!Before submission, document the exact versions:
cd your-project/utils
git describe --tags --always > ../UTILS_VERSION.txt
cd ..
# Add to README.md or paper repository
echo "Analysis uses my-utils version: $(cat UTILS_VERSION.txt)"git submodule update --init --recursivecd myutils
git fetch origin
git pull origin main
cd ..
git add myutils
git commit -m "Update submodule reference"# See history
git log -- myutils
# Reset to previous commit
git checkout <previous-commit-hash> -- myutils
git commit -m "Revert myutils to previous version"# Usually safe to accept incoming changes
git checkout --theirs .gitmodules
git add .gitmodules- Always tag versions in
myutilsbefore using in production analysis - Lock projects to specific tags for published work
- Document which version you used in your project README
- Test changes to utils in one project before updating others
- Commit submodule updates separately from other changes
If you already have projects with copied utils:
cd /home/workspace/old-project
# Remove old utils copy
rm -rf utils/
git add utils/
git commit -m "Remove old utils copy"
# Add as submodule
git submodule add https://github.com/sangmk/myutils.git myutils
git add .gitmodules myutils
git commit -m "Convert myutils to submodule"| Action | Command |
|---|---|
| Add submodule | git submodule add <url> myutils |
| Clone with submodules | git clone --recursive <url> |
| Initialize submodules | git submodule update --init --recursive |
| Update to latest | cd myutils && git pull && cd .. && git add myutils |
| Lock to version | cd myutils && git checkout v0.2.0 && cd .. && git add myutils |
| Check version | cd myutils && git describe --tags |
Key Benefit: Each project is self-contained with versioned, traceable dependencies while avoiding code duplication.