| name | git-repository |
| description | Repository management strategies including branch strategies (Git Flow, GitHub Flow, trunk-based), monorepo patterns, submodules, and repository organization. Use when user needs guidance on repository structure or branching strategies. |
Git Repository Management Skill
This skill provides comprehensive guidance on repository management strategies, branching models, repository organization patterns, and scaling git for large teams and codebases.
When to Use
Activate this skill when:
- Setting up new repository structure
- Choosing branching strategy
- Managing monorepo vs polyrepo
- Organizing multi-project repositories
- Implementing submodule or subtree strategies
- Scaling git for large teams
- Migrating repository structures
- Establishing team workflows
Branching Strategies
Git Flow
Branch Structure:
main (or master) - Production releases only
develop - Integration branch for next release
feature/* - Feature development branches
release/* - Release preparation branches
hotfix/* - Emergency production fixes
Workflow:
git checkout develop
git checkout -b feature/user-authentication
git commit -m "feat: add JWT authentication"
git checkout develop
git merge --no-ff feature/user-authentication
git branch -d feature/user-authentication
git checkout develop
git checkout -b release/v1.2.0
git commit -m "chore: prepare release v1.2.0"
git checkout main
git merge --no-ff release/v1.2.0
git tag -a v1.2.0 -m "Release version 1.2.0"
git checkout develop
git merge --no-ff release/v1.2.0
git branch -d release/v1.2.0
git push origin main develop --tags
git checkout main
git checkout -b hotfix/security-patch
git commit -m "fix: patch security vulnerability"
git checkout main
git merge --no-ff hotfix/security-patch
git tag -a v1.2.1 -m "Hotfix v1.2.1"
git checkout develop
git merge --no-ff hotfix/security-patch
git branch -d hotfix/security-patch
git push origin main develop --tags
Best For:
- Scheduled releases
- Multiple production versions
- Large teams with QA process
- Products with maintenance windows
- Enterprise software
Drawbacks:
- Complex workflow
- Long-lived branches
- Potential merge conflicts
- Delayed integration
GitHub Flow
Branch Structure:
main - Production-ready code (always deployable)
feature/* - All feature and fix branches
Workflow:
git checkout main
git pull origin main
git checkout -b feature/add-api-logging
git commit -m "feat: add structured logging middleware"
git push -u origin feature/add-api-logging
git checkout main
git pull origin main
git branch -d feature/add-api-logging
Best For:
- Continuous deployment
- Small to medium teams
- Web applications
- Rapid iteration
- Cloud-native applications
Drawbacks:
- Requires robust CI/CD
- No release staging
- Less structured than Git Flow
Trunk-Based Development
Branch Structure:
main (or trunk) - Single source of truth
- Short-lived feature branches (< 2 days, optional)
- Feature flags for incomplete work
Workflow:
git checkout main
git pull origin main
git commit -m "fix: correct validation logic"
git push origin main
git checkout -b optimize-query
git commit -m "perf: optimize database query"
git push -u origin optimize-query
git checkout main
git commit -m "feat: add payment gateway (behind feature flag)"
git push origin main
Best For:
- High-velocity teams
- Continuous integration
- Automated testing
- Feature flag infrastructure
- DevOps culture
Drawbacks:
- Requires discipline
- Needs comprehensive tests
- Feature flag management
- Higher deployment frequency
Release Branch Strategy
Branch Structure:
main - Current development
release/v* - Long-lived release branches
feature/* - Feature branches
Workflow:
git checkout -b release/v1.0 main
git push -u origin release/v1.0
git checkout main
git checkout release/v1.0
git cherry-pick abc123
git push origin release/v1.0
git tag -a v1.0.5 -m "Patch release v1.0.5"
git push origin v1.0.5
git checkout release/v0.9
git cherry-pick def456
git tag -a v0.9.8 -m "Security patch v0.9.8"
Best For:
- Multiple product versions
- Long-term support releases
- Enterprise customers
- Regulated industries
Drawbacks:
- Maintenance overhead
- Complex cherry-picking
- Diverging codebases
Feature Branch Workflow
Branch Structure:
main - Stable production code
feature/* - Feature branches from main
bugfix/* - Bug fix branches
Workflow:
git checkout main
git checkout -b feature/payment-integration
git fetch origin
git rebase origin/main
git merge origin/main
git push origin feature/payment-integration
Best For:
- Medium-sized teams
- Code review processes
- Parallel feature development
- Quality gates before merge
Repository Organization
Monorepo
Structure:
monorepo/
├── .git/
├── services/
│ ├── api/
│ ├── web/
│ └── worker/
├── packages/
│ ├── shared-utils/
│ ├── ui-components/
│ └── api-client/
├── tools/
│ ├── build-tools/
│ └── scripts/
└── docs/
Advantages:
- Single source of truth
- Shared code visibility
- Atomic cross-project changes
- Unified versioning
- Simplified dependency management
- Consistent tooling
Disadvantages:
- Large repository size
- Slower clone/fetch
- Complex CI/CD
- Access control challenges
- Tooling requirements
Implementation:
git init
mkdir -p services/api services/web packages/shared-utils
cat > package.json << EOF
{
"name": "monorepo",
"private": true,
"workspaces": [
"services/*",
"packages/*"
]
}
EOF
git clone --filter=blob:none --no-checkout <url>
cd repo
git sparse-checkout init --cone
git sparse-checkout set services/api packages/shared-utils
git checkout main
git diff --name-only HEAD~1 | grep "^services/api" && cd services/api && npm run build
Tools:
- Bazel - Build system for large monorepos
- Nx - Monorepo build system (Node.js)
- Lerna - JavaScript monorepo management
- Turborepo - High-performance build system
- Git-subtree - Merge external repositories
Polyrepo
Structure:
organization/
├── api-service/ (separate repo)
├── web-app/ (separate repo)
├── mobile-app/ (separate repo)
├── shared-utils/ (separate repo)
└── documentation/ (separate repo)
Advantages:
- Clear ownership boundaries
- Independent versioning
- Smaller repository size
- Granular access control
- Flexible CI/CD
- Team autonomy
Disadvantages:
- Dependency version conflicts
- Cross-repo changes are complex
- Duplicated tooling/config
- Harder to refactor across repos
Implementation:
git clone git@github.com:org/template-service.git new-service
cd new-service
rm -rf .git
git init
git remote add origin git@github.com:org/new-service.git
git submodule add git@github.com:org/shared-config.git config
Monorepo vs Polyrepo Decision Matrix
| Factor | Monorepo | Polyrepo |
|---|
| Team Size | Large teams | Small, autonomous teams |
| Code Sharing | High code reuse | Limited sharing |
| Deployment | Coordinated releases | Independent deployments |
| Access Control | Coarse-grained | Fine-grained |
| Repository Size | Very large | Small to medium |
| CI/CD Complexity | High | Low to medium |
| Tooling Requirements | Specialized tools | Standard git tools |
| Refactoring | Easy cross-project | Complex cross-repo |
Submodule Management
Basic Submodules
git submodule add https://github.com/org/shared-lib.git libs/shared
git clone --recurse-submodules <url>
git submodule init
git submodule update
cd libs/shared
git pull origin main
cd ../..
git add libs/shared
git commit -m "chore: update shared library"
git submodule update --remote --merge
git submodule deinit libs/shared
git rm libs/shared
rm -rf .git/modules/libs/shared
Submodule Strategies
Pinned Version Strategy:
git submodule update --remote libs/shared
git add libs/shared
git commit -m "chore: update shared-lib to v1.2.3"
Auto-Update Strategy:
name: Update Submodules
on:
schedule:
- cron: '0 0 * * 0'
jobs:
update:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: true
- run: git submodule update --remote
- run: git commit -am "chore: update submodules"
- run: git push
Nested Submodules
cd libs/framework
git submodule add https://github.com/org/utils.git utils
git submodule update --init --recursive
git submodule foreach 'git checkout main'
git submodule foreach 'git pull'
git submodule foreach --recursive 'echo $name: $(git rev-parse HEAD)'
Subtree Management
Git Subtree vs Submodule
Subtree Advantages:
- Simpler for contributors
- No separate clone steps
- Part of main repository history
- No broken references
Subtree Disadvantages:
- More complex to update
- Pollutes main history
- Larger repository
Subtree Operations
git subtree add --prefix=libs/shared https://github.com/org/shared.git main --squash
git subtree pull --prefix=libs/shared https://github.com/org/shared.git main --squash
git subtree push --prefix=libs/shared https://github.com/org/shared.git feature-branch
git subtree split --prefix=libs/shared -b shared-lib-branch
git push git@github.com:org/new-shared-lib.git shared-lib-branch:main
Subtree Workflow
git remote add shared-lib https://github.com/org/shared.git
git subtree add --prefix=libs/shared shared-lib main --squash
git fetch shared-lib
git subtree pull --prefix=libs/shared shared-lib main --squash
git subtree push --prefix=libs/shared shared-lib feature-branch
Repository Templates
GitHub Template Repository
mkdir -p .github/workflows
cat > .github/workflows/ci.yml << EOF
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: make test
EOF
touch .gitignore README.md LICENSE CONTRIBUTING.md
mkdir -p docs src tests
Cookiecutter Template
pip install cookiecutter
cookiecutter https://github.com/org/project-template.git
project-template/
├── cookiecutter.json
└── {{cookiecutter.project_name}}/
├── .git/
├── src/
├── tests/
└── README.md
Large Repository Management
Partial Clone
git clone --filter=blob:none <url>
git clone --filter=tree:0 <url>
git clone --depth 1 <url>
git clone --depth 1 --single-branch --branch main <url>
Sparse Checkout
git sparse-checkout init --cone
git sparse-checkout set src/api src/shared
git sparse-checkout add docs
git sparse-checkout list
git sparse-checkout disable
Git LFS (Large File Storage)
git lfs install
git lfs track "*.psd"
git lfs track "*.zip"
git lfs track "data/**"
cat .gitattributes
git clone <url>
cd repo
git lfs pull
git lfs migrate import --include="*.zip"
Repository Splitting
Extract Subdirectory to New Repo
git filter-repo --path services/api --path-rename services/api:
git subtree split --prefix=services/api -b api-service
mkdir ../api-service
cd ../api-service
git init
git pull ../original-repo api-service
Merge Multiple Repos
git remote add project-b ../project-b
git fetch project-b
git merge --allow-unrelated-histories project-b/main
mkdir project-b
git mv * project-b/
git commit -m "chore: organize project-b into subdirectory"
Repository Maintenance
Regular Maintenance Tasks
git gc --aggressive
git prune --expire now
git fsck --full
git repack -a -d --depth=250 --window=250
git update-server-info
Automation Script
#!/bin/bash
echo "Starting repository maintenance..."
git fetch --all --prune
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs -r git branch -D
git gc --auto
git fsck --full --strict
echo "Maintenance complete!"
Scheduled Maintenance
name: Repository Maintenance
on:
schedule:
- cron: '0 2 * * 0'
jobs:
maintain:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Run Maintenance
run: |
git gc --aggressive
git prune --expire now
git fsck --full
Access Control and Permissions
Branch Protection Rules
protected_branches:
main:
required_pull_request_reviews:
required_approving_review_count: 2
dismiss_stale_reviews: true
require_code_owner_reviews: true
required_status_checks:
strict: true
contexts:
- continuous-integration
- security-scan
enforce_admins: true
restrictions:
users: []
teams: [core-team]
CODEOWNERS File
* @org/core-team
/services/api/ @org/backend-team
/services/web/ @org/frontend-team
/services/mobile/ @org/mobile-team
/docs/ @org/documentation-team
/.github/ @org/devops-team
/security/ @org/security-team @org/lead-architect
/packages/shared/ @org/core-team @org/architecture-team
Migration Strategies
SVN to Git
svn log --quiet | grep "^r" | awk '{print $3}' | sort -u > authors.txt
git svn clone <svn-url> --authors-file=authors.txt --stdlayout repo
cd repo
git for-each-ref --format="%(refname:short)" refs/remotes/tags | \
cut -d / -f 3 | xargs -I {} git tag {} refs/remotes/tags/{}
git remote add origin <git-url>
git push -u origin --all
git push origin --tags
Mercurial to Git
hg bookmark -r default main
hg push git+ssh://git@github.com/org/repo.git
git clone https://github.com/frej/fast-export.git
mkdir git-repo && cd git-repo
git init
../fast-export/hg-fast-export.sh -r ../hg-repo
git checkout HEAD
Best Practices
- Choose Appropriate Strategy: Match branching model to team size and deployment frequency
- Document Workflows: Keep team documentation current
- Automate Maintenance: Regular repository health checks
- Use Branch Protection: Enforce code review and CI
- Clear Ownership: Define code owners for all areas
- Regular Cleanup: Remove stale branches and merged features
- Monitor Repository Size: Use LFS for large files
- Template Repositories: Standardize new project structure
- Access Control: Implement principle of least privilege
- Migration Planning: Test migrations thoroughly before production
Resources
Additional repository management resources are available in the assets/ directory:
templates/ - Repository structure templates
scripts/ - Automation and maintenance scripts
workflows/ - CI/CD workflow examples
See references/ directory for:
- Branching strategy comparison guides
- Monorepo tool documentation
- Enterprise git patterns
- Repository scaling strategies